Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式匹配函数参数_Python_Regex - Fatal编程技术网

Python 正则表达式匹配函数参数

Python 正则表达式匹配函数参数,python,regex,Python,Regex,成为: select[ 1, 22 ,word, two words ] 排序后,希望处理参数周围的可选单引号 感谢您的考虑您没有指定编程语言,但对于python,您可以使用: param1: "1" param2: "22" param3: "word" param4: "two words" C对于咯咯笑: import re string = re.sub(r"select\[\s+|\]", "", "select[ 1, 22 ,word, two w

成为:

select[  1, 22 ,word,      two words    ]
排序后,希望处理参数周围的可选单引号


感谢您的考虑

您没有指定编程语言,但对于python,您可以使用:

param1: "1"
param2: "22"
param3: "word"
param4: "two words"
C对于咯咯笑:

import re
string = re.sub(r"select\[\s+|\]", "", "select[  1, 22 ,word,      two words    ]")
final, n = "", 1
for p in [p.strip() for p in string.split(",")]:
    final += 'param{}:"{}", '.format(n,p)
    n += 1
print final.rstrip(", ")
# param1:"1", param2:"22", param3:"word", param4:"two words"

下面是一个Perl解决方案:

严格使用; 使用警告; my$str=选择[1,22,单词,两个单词]; 如果$str=~m{\b选择\[\s*}xg{ 我的@param; 而$str=~m{\w+?:\s+\w+*|'[^']*'\s*}xg{ 推送@param$1; $str=~m{\s*}xgc 或最后; } 如果$str=~m{\]}xg{ 为@param打印$\n; } } 输出:

1. 22 单词 两个字
它尝试稍微验证输入,即它不会为格式错误的字符串生成输出,并且它已经解析单引号参数。

另一个用于支持\G的引擎:

Highjacking您的演示: 您可能希望擦除两侧的空白,这可以使用ie Python完成:


不可能用重复的捕获组捕获所有值。只有最后一个值将保留在组内存缓冲区中。你采用的方法行不通。除非你在C/Net或PyPiReX Python模块中使用它。请考虑使用Python完成的Kiki/KiKi-Re.在Linux下,您可以从适当的打包系统安装它。真是巧合@在我发布答案整整四天后,python标签被添加了。您可能会注意到其他答案中有一个明确提到这一点,第三个答案使用C。
import re
string = re.sub(r"select\[\s+|\]", "", "select[  1, 22 ,word,      two words    ]")
final, n = "", 1
for p in [p.strip() for p in string.split(",")]:
    final += 'param{}:"{}", '.format(n,p)
    n += 1
print final.rstrip(", ")
# param1:"1", param2:"22", param3:"word", param4:"two words"
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        Regex regex = new Regex(@"[a-zA-Z0-9 ]*(?=\s*[,\]])");

        string test = @"select[  1, 22 ,word,      two words    ]";

        MatchCollection matches = regex.Matches(test);

        IEnumerable<string> items = matches.Cast<Match>().Select(x => x.Value.Trim());

        items.ToList().ForEach(x => Console.WriteLine(x));

    }
}
(?:\G(?!\A)|select\[)        # look for the last match or select[
\s*                          # whitespaces, optional and greedy
((?:(?!(?:[ ]{2,}|\]|,)).)+) # not overrunning two consecutive spaces, ] or ,
\s*                          # another greedy whitespace
(?:,|\])                     # , or ]
import regex as re
rx = re.compile(r'''
        (?:\G(?!\A)|select\[)
        \s*
        ((?:(?!(?:[ ]{2,}|\]|,)).)+)
        \s*
        (?:,|\])
''', re.VERBOSE)

params = [match.group(1).strip()
          for match in rx.finditer(string)]
print(params)
# ['1', '22', 'word', 'two words']