我有以下正则表达式:
/(?:[\[\{]*)(?:([A-G\-][^A-G\]\}]*)+)(?:[\]\}]*)/
使用以下表达式:
{A''BsCb}
我期望 3 个匹配的结果
A''
Bs
Cb
但在 https://regex101.com/ 进行测试只给我最后一个匹配Cb
,并告诉我重复捕获组只会捕获最后一次迭代,在重复组周围放置一个捕获组。
我以为这就是我所做的!我以为我已经理解了这里描述的问题 http://www.regular-expressions.info/captureall.html 因此,我的 + 外面的括号内有捕获组。
但要么为时已晚,要么我需要一个不会在提到正则表达式时头脑崩溃的人来告诉我哪里出了问题。
最佳答案
您可以使用此模式在第 0 项处使用 preg_match_all
获取它:
~
(?:
\G (?!\A) # contiguous to previous match, but not at the start of the string
|
{ (?=[^}]* }) # start with { and check if a closing bracket follows
|
\[ (?=[^]]* ]) # the same for square bracket
)
\K # start the match result here
[A-G] [^]A-G}]*
~xS
关于php - 由于重复捕获组而不是捕获重复组,正则表达式不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39155242/