正则表达式匹配除 2 个连续花括号之外的任何内容
匹配除 2 个连续大括号 ({) 之外的任何内容的正则表达式是什么?
示例字符串:
{{some text}} string 我想要{{another set {{and inside}} }}
我只想获取我想要的字符串
。
我曾想过使用堆栈来做这些事情,但我想知道这是否可以使用正则表达式来完成。
我正在使用 PHP 的 PCRE
提前致谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用前瞻断言
(?!{{|}})
来验证您的外部大括号内没有嵌套的大括号组。测试程序
输出
它似乎也合理地处理了各种边缘情况:
Use a lookahead assertion
(?!{{|}})
to verify that you don't have a nested set of braces inside of your outer set.Test program
Output
It appears to handle various edge cases reasonably, as well:
如果您需要对内容执行更复杂的操作,例如处理内容或变量,那么您可以使用递归正则表达式,利用 (?R) 运算符。
这会将:
{{abcde{{fg{{hi}}jk}}lm}}
转换为(2:; )jk>)lm>)
关于正则表达式的一些解释:
#\{\{((?:[^(\{\{)(\}\})]+|(? R))+)\}\}#
前后双大括号匹配任意目标组件,大括号的内容为两个已定义选项中的一个或多个:
a string with没有双大括号
[^(\{\{)(\}\})]+
整个正则表达式重复。
(?:)
括号是非捕获组。注意。 #s 是模式分隔符,我认为额外的斜杠会进一步降低可读性。
If you need to do something more complicated with the contents, such as processing the contents or the variables, then you can use a recursive regexp, making use of the (?R) operator.
This converts:
{{abcde{{fg{{hi}}jk}}lm}}
to(2:<abcde(1:<fg(0:<hi>)jk>)lm>)
A bit of explanation on the regexp:
#\{\{((?:[^(\{\{)(\}\})]+|(?R))+)\}\}#
The double braces at the front and back match any target component, the contents of the braces are to be one or more of the two defined options:
a string with no double braces
[^(\{\{)(\}\})]+
the whole regexp repeated. The
(?:)
bracket is a non-capturing group.NB. The
#s
are the pattern delimiters, I thought extra slashes would decrease readability further.