Javascript 正则表达式。在满足条件之前避免匹配字符串?
需要正则表达式的帮助 字符串: (可以包含更多级别的“测试”) 应匹配: 一, 二, 问题: 如何在PHP中编写符合所需结果的正则表达式 下面是我的例子:Javascript 正则表达式。在满足条件之前避免匹配字符串?,javascript,php,regex,Javascript,Php,Regex,需要正则表达式的帮助 字符串: (可以包含更多级别的“测试”) 应匹配: 一, 二, 问题: 如何在PHP中编写符合所需结果的正则表达式 下面是我的例子: 您需要使用递归模式: ~\[test](?:[^[]+|\[(?!/?test])|(?R))*+\[/test]~ 详情: ~ # pattern delimiter \[test] (?: # non-capturing group (possible cont
您需要使用递归模式:
~\[test](?:[^[]+|\[(?!/?test])|(?R))*+\[/test]~
详情:
~ # pattern delimiter
\[test]
(?: # non-capturing group (possible content between test tags)
[^[]+ # all that is not a [
|
\[(?!/?test]) # a [ not part of a test tag (opening or closing)
|
(?R) # repeat the whole pattern (recursion)
)*+ # repeat the group zero or more times (possessive quantifier)
\[/test]
~
注意:这种方法只有在PHP或Perl中才可能实现,Javascript没有递归功能。问题是?您是否希望在Javascript或PHP中得到答案?需要在PHP中得到答案。卡西米尔和希波利特的回答很好。
[test]
[inner]*anything*
[/inner]
[/test]
~\[test](?:[^[]+|\[(?!/?test])|(?R))*+\[/test]~
~ # pattern delimiter
\[test]
(?: # non-capturing group (possible content between test tags)
[^[]+ # all that is not a [
|
\[(?!/?test]) # a [ not part of a test tag (opening or closing)
|
(?R) # repeat the whole pattern (recursion)
)*+ # repeat the group zero or more times (possessive quantifier)
\[/test]
~