正则表达式捕获组多次匹配

4
我希望有一个正则表达式可以在两个分隔符之间捕获所有模式的实例。
简化为单字符模式的示例(目标:捕获第一个x和最后一个z之间的所有b):
bbxabbcbacbedbzbb
    ^^ ^  ^  ^  

需要捕获5个 b

我尝试了 .*x.*(b).*z.*,但只捕获最后一个 b。(rubular


1
必须重新打开,因为thisthis匹配最接近的xz之间,但在当前问题中,匹配必须出现在第一个x和最后一个z之间。 - Ryszard Czech
1个回答

4

使用

(?:x|(?<!\A)\G).*?\Kb(?=.*z)

参见正则表达式证明

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    x                        'x'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
      \A                       the beginning of the string
--------------------------------------------------------------------------------
    )                        end of look-behind
--------------------------------------------------------------------------------
    \G                       where the last m//g left off
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  \K                       match reset operator (omits matched text)
--------------------------------------------------------------------------------
  b                        'b'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*                       any character except line breaks (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    z                        'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead

你被讨论在 Meta 上了,参见 https://meta.stackoverflow.com/questions/406320。 - Zombo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接