如何匹配递归模式 `a(?R)b` 包括另一个字符串,例如"test{aaabbb}"? (正则表达式)
How to match the recursive pattern `a(?R)b` including another string e.g. "test{aaabbb}"? (Regex)
根据,如果我的字符串不像
那样简单,如何使用递归模式
aaaaabbbbb
aabb
aaabbb
但他们包括另一个文本,例如
test{aaaaabbbbb}
test{aabb}
test{aaabbb}
这里我想匹配一个递归模式,包括 test{...}
.
确定 regex:test\{a(?R)*b\}
不应该工作。
请注意,我更喜欢 PCRE 形式的正则表达式
您需要更换 recursive regex with subroutine call:
test\{(a(?1)*b)\}
见demo
These are very similar to regular expression recursion. Instead of matching the entire regular expression again, a subroutine call only matches the regular expression inside a capturing group. You can make a subroutine call to any capturing group from anywhere in the regex. If you place a call inside the group that it calls, you'll have a recursive capturing group.
此外,请检查 Matching Balanced Constructs 部分。
当然,您需要使用 命名子例程调用 以避免回避影响整个表达式,
例如。 test\{(?<x>a(?&x)?b)\}
根据
aaaaabbbbb
aabb
aaabbb
但他们包括另一个文本,例如
test{aaaaabbbbb}
test{aabb}
test{aaabbb}
这里我想匹配一个递归模式,包括 test{...}
.
确定 regex:test\{a(?R)*b\}
不应该工作。
请注意,我更喜欢 PCRE 形式的正则表达式
您需要更换 recursive regex with subroutine call:
test\{(a(?1)*b)\}
见demo
These are very similar to regular expression recursion. Instead of matching the entire regular expression again, a subroutine call only matches the regular expression inside a capturing group. You can make a subroutine call to any capturing group from anywhere in the regex. If you place a call inside the group that it calls, you'll have a recursive capturing group.
此外,请检查 Matching Balanced Constructs 部分。
当然,您需要使用 命名子例程调用 以避免回避影响整个表达式,
例如。 test\{(?<x>a(?&x)?b)\}