在没有递归或平衡组的情况下匹配嵌套括号
挑战:
匹配一组使用缺少递归和平衡组支持的正则表达式风格任意嵌套括号,例如 Java 的 java.util.regex,捕获给定字符串内的三个外部组:
(F(i(r(s)t))) ((S)(e)((c)(o))(n)d) (((((((Third)))))))
解决方案:将引用转发到救援
与普遍看法相反,使用前向引用可以匹配没有这些高级功能的嵌套括号:
(?=\()(?:(?=.*?\((?!.*?)(.*\)(?!.*).*))(?=.*?\)(?!.*?)(.*)).)+?.*?(?=)[^(]*(?=$)
分解:
这个复杂的正则表达式分两个阶段运行:
工作原理:
内部组匹配变体:
对于匹配内部组,策略保持不变,但使用捕获组将匹配的内容保存在一对平衡的括号内:
(?=\()(?=((?:(?=.*?\((?!.*?)(.*\)(?!.*).*))(?=.*?\)(?!.*?)(.*)).)+?.*?(?=)[^(]*(?=$)))
全面破坏:
表格总结了正则表达式的组件和功能:
Note | Component | Description |
---|---|---|
(?=() | Look for '(' | |
(?: | Start group for iteration | |
(?=.?((?!.?1)) | Look for '(' not followed by 1, which contains the matched inner content | |
(.)(?!.2).*)) | Capture inner content and check for at least one more ')' | |
(?=.?)(?!.?3)) | Look for ')' not followed by 2, which contains the matched outer content | |
(. ) | Capture outer content | |
. | Consume a character | |
) | Close group | |
? | Match as few times as possible | |
.*?(?=1) | Match up to and including the last '(' | |
1*(?=2$) | Match up to the last ')' without encountering more '(' |
以上是如何在没有递归或平衡组的情况下匹配嵌套括号?的详细内容。更多信息请关注PHP中文网其他相关文章!