使用正则表达式在双花括号内获取值 [英] Get values inside double curly braces with regex
问题描述
通过此字符串:
dfasd {{test}} asdhfj {{te{st2}} asdfasd {{te}st3}}
我想获得以下子字符串:
I would like to get the following substrings:
test, te{st2, te}st3
换句话说,我想将所有内容都保留在双花括号中,包括单花括号.
In other words I want keep everything inside double curly braces including single curly braces.
我不能使用这种模式:
{{(.*)}}
因为它与第一个{{和最后一个}}之间的整个内容匹配:
because it matches the whole thing between first {{ and last }}:
test}} asdhfj {{te{st2}} asdfasd {{te}st3
我设法用此正则表达式模式获得了前两个:
I managed to get the first two with this regex pattern:
{{([^}]*)}}
有什么办法可以使用正则表达式来获取全部三个?
Is there any way to get all three using regex?
推荐答案
尝试 {{(.*?)}}
.
.*?
意味着在}}匹配后立即进行懒惰/非贪婪搜索=>,它将捕获找到的文本并停止查找.否则,它将进行贪婪的搜索,因此从第一个{{开始,最后一个}}开始.
.*?
means to do a lazy / non greedy search => as soon as }} matches, it will capture the found text and stop looking. Otherwise it will do a greedy search and therefore start with the first {{ and end with the very last }}.
这篇关于使用正则表达式在双花括号内获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!