[英]Regular Expression, match characters between { }
I'm trying to find text that contains a < or > between { }. 我正在尝试查找在{}之间包含<或>的文本。 This is within HTML and I'm having trouble getting it to be "ungreedy". 这是在HTML内,我很难使它变得“笨拙”。
So I want to find text that matches these strings: 所以我想找到与这些字符串匹配的文本:
{test > 3}
{testing >= 3 : comment}
{example < 4}
I've tried a number of regular expressions, but the all seem to continue past the closing } and including HTML that has < or >. 我已经尝试了许多正则表达式,但是所有这些似乎都在结束}之后并包括带有<或>的HTML。 For example, I tried this regex 例如,我尝试了此正则表达式
{.*?(<|>).*?}
but that ends up matching text like this: 但是最终匹配这样的文本:
{if true}<b>testing</b>{/if}
It seems pretty simple, any text between { } that contain < or >. 看起来很简单,{}之间的任何包含<或>的文本。
这应该可以解决问题:
{[^}]*(<|>).*}
An even more efficient regex (because there is no non-greedy matching): 更加高效的正则表达式(因为没有非贪婪匹配):
'#{[^}<>]*[<>]+[^}]*}#'
The reason there aren't brackets in the third character class is so that it matches strings with more than one > (such as {foo <> bar}
... 第三个字符类中没有括号的原因是,它匹配具有多个>的字符串(例如{foo <> bar}
...
{[^}]*?(<|>)[^{]*?}
Try that. 试试看 Note that I replaced the .
请注意,我替换了.
s with a character class that means everything except the left/right curly braces. 的字符类表示除左/右花括号外的所有内容。
您是否尝试过使用“不满意(U)”开关?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.