正则表达式匹配函数参数

时间:2017-04-30 16:35:03

标签: python regex

select\[([^\s]*(?<param>[a-z0-9]+)[^,\s]*)*\]

尝试从逗号分隔的参数加载捕获组(数字或字母数字选项),忽略每个参数的前导/尾随空格,但保留单词之间的空格(即“两个单词”)。

select[  1, 22 ,word,      two words    ]

成为:

param1: "1"
param2: "22"
param3: "word"
param4: "two words"

http://symfony.com/doc/current/security.html

一旦那个排序,就想在参数周围处理可选的单引号。

感谢您的考虑

4 个答案:

答案 0 :(得分:1)

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

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"

Python Demo

答案 1 :(得分:1)

C#for giggles:

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));

    }
}

答案 2 :(得分:1)

另一个支持\G的引擎:

(?:\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 ]

劫持您的演示: https://regex101.com/r/a0ab0Q/8

<小时/> 你可能想要删除两边的空白(使用ie Python):

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']

答案 3 :(得分:-2)

这是一个Perl解决方案:

use strict;
use warnings;

my $str = "select[  1, 22 ,word,      two words    ]";

if ($str =~ m{ \b select \[ \s* }xg) {
    my @param;
    while ($str =~ m{ ( \w+ (?: \s+ \w+ )* | ' [^']* ' ) \s* }xg) {
        push @param, $1;
        $str =~ m{ , \s* }xgc
            or last;
    }
    if ($str =~ m{ \] }xg) {
        print "$_\n" for @param;
    }
}

输出:

1
22
word
two words

它尝试稍微验证输入(即它不会为格式错误的字符串生成输出)并且它已经解析了单引号参数。

相关问题