简体   繁体   中英

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".

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 >. 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} ...

{[^}]*?(<|>)[^{]*?}

Try that. Note that I replaced the . s with a character class that means everything except the left/right curly braces.

您是否尝试过使用“不满意(U)”开关?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM