简体   繁体   English

正则表达式 - 匹配可选重复的捕获组模式

[英]Regex - match an optionally repeated pattern of capture groups

Appologies for not knowing exactly how to word this question.很抱歉不知道该如何表达这个问题。 There is probably even a better title.可能还有更好的标题。 I'm open to suggestions.我愿意接受建议。

I have the following subjects:我有以下主题:

(Field1 = 'Value1') and (Field2 = 'Value2')

and

(Field1 = 'Value1') and (Field2 = 'Value2') or (Field3 = 'Value3')

I want to match in such a way that I have each thing between the () in groups and each conjunction in a group.我想以这样一种方式进行匹配,即我在组中的 () 和组中的每个连词之间都有每件事。 So, for the second one, some variation of所以,对于第二个,一些变化

0: Field1 = 'Value1'
1: and
2: Field2 = 'Value2'
3: or
4: Field3 = 'Value3'

The good news is, I've got regex that works on the first:好消息是,我有一个适用于第一个的正则表达式:

\(([A-Za-z0-9\s\'=]+)\) (and|or) \(([A-Za-z0-9\s\'=]+)\)

https://regex101.com/r/hMXAXS/1 https://regex101.com/r/hMXAXS/1

But (on the second subject) it doesn't match the third "and ()".但是(在第二个主题上)它与第三个“和()”不匹配。 I need to support arbitrary numbers of groups.我需要支持任意数量的组。 I can modify it to just look for "and ()" but then it doesn't match the first group.我可以将其修改为仅查找“和 ()”,但它与第一组不匹配。

How can I tell regex to do this?我怎样才能告诉正则表达式这样做? I either need to "double count" some groups (which is fine) or have some other way of optionally looking for additional patterns and matching them.我要么需要“重复计算”一些组(这很好),要么有一些其他方式来选择性地查找其他模式并匹配它们。

Thanks for the help!谢谢您的帮助!

PS: I was able to get my application to work with the regex ((and|or) \(([A-Za-z0-9\s\'=]+)\))+ and then just accepting that the first group would never match and creating application logic to support this. PS:我能够让我的应用程序使用正则表达式((and|or) \(([A-Za-z0-9\s\'=]+)\))+然后只接受第一个group 永远不会匹配并创建应用程序逻辑来支持它。 Still, I'd bet there's a better way.尽管如此,我敢打赌还有更好的方法。

You may use preg_match_all here with the regex pattern (?<=\()(.*?)(?=\))|(?:and|or) as follows:您可以在此处将preg_match_all与正则表达式模式(?<=\()(.*?)(?=\))|(?:and|or)一起使用,如下所示:

$input = "(Field1 = 'Value1') and (Field2 = 'Value2') or (Field3 = 'Value3')";
preg_match_all("/(?<=\()(.*?)(?=\))|(?:and|or)/", $input, $matches);
print_r($matches[0]);

This prints:这打印:

Array
(
    [0] => Field1 = 'Value1'
    [1] => and
    [2] => Field2 = 'Value2'
    [3] => or
    [4] => Field3 = 'Value3'
)

If you are not worried about fringe cases where delimiting words or paretheses can exist within the parenthetical expressions, then preg_split() generates the desired flat array.如果您不担心括号表达式中可能存在定界词或括号的边缘情况,那么preg_split()会生成所需的平面数组。

Code: ( Demo )代码:(演示

$input = "(Field1 = 'Val and ue1') and (Field2 = 'Valu or e2') or (Field3 = 'Value3')";
var_export(
    preg_split(
        "~^\(|\)$|\) (and|or) \(~",
        $input,
        0,
        PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE
    )
);

Output: Output:

array (
  0 => 'Field1 = \'Val and ue1\'',
  1 => 'and',
  2 => 'Field2 = \'Valu or e2\'',
  3 => 'or',
  4 => 'Field3 = \'Value3\'',
)

Or simplify the pattern by pre-trimming the outermost parentheses.或者通过预先修剪最外面的括号来简化图案。 ( Demo ) 演示

var_export(preg_split("~\) (and|or) \(~", trim($input, '()'), 0, PREG_SPLIT_DELIM_CAPTURE));

You can also use the continue metacharacter \G to continue matching from the end of the previous match: ( Demo ) This takes 88 steps versus Tim's pattern which takes 280 steps to parse the string.您还可以使用继续元字符\G从上一个匹配项的末尾继续匹配:(演示)这需要88 个步骤,而 Tim 的模式需要 280 个步骤来解析字符串。

$input = "(Field1 = 'Val and ue1') and (Field2 = 'Valu or e2') or (Field3 = 'Value3')";
preg_match_all('~(?:^\(|\G(?!^)(?:\) | \())\K(?:(?:and|or)|[^)]+)~', $input, $m);
print_r($m[0]);

Edit after the asker accepted an answer that does not provide the output array structure stated in the question: ( Demo )在提问者接受了不提供问题中所述的 output 数组结构的答案后进行编辑:(演示

preg_match_all("~\((\S+ = '.*?')\) ?(or|and)?~", $input, $m, PREG_SET_ORDER);
print_r($m);

This does not check that a parenthetical expression occurs after a conjunction.这不会检查括号表达式是否出现在连词之后。 Also, when iterating the matches an extra check will be required to see if the third group ( [2] ) is declared.此外,在迭代匹配时,将需要进行额外检查以查看是否声明了第三组 ( [2] )。

Array
(
    [0] => Array
        (
            [0] => (Field1 = 'Val and ue1') and
            [1] => Field1 = 'Val and ue1'
            [2] => and
        )

    [1] => Array
        (
            [0] => (Field2 = 'Valu or e2') or
            [1] => Field2 = 'Valu or e2'
            [2] => or
        )

    [2] => Array
        (
            [0] => (Field3 = 'Value3')
            [1] => Field3 = 'Value3'
        )
)

If you are OK with getting three groups per match...如果您同意每场比赛获得三个小组......

1 = key 2 = value 3 = conjunction verb 1 = 键 2 = 值 3 = 连词

Then this regex will also allow parenthesis in the value.那么这个正则表达式也将允许在值中使用括号。

/\((.*?) = '(.*?)'\) ?(and|or)?/gm

Which results in these matches for this string...这会导致此字符串的这些匹配...

(Field1 = 'Value1') and (Field2 = '(in parenthesis)') and (Field3 = 'Value3')

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM