开发者

Regular Expression, match characters between { }

开发者 https://www.devze.com 2023-01-09 00:49 出处:网络
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\".

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开发者_Go百科}
{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 >.


This should do the trick:

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


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.


Have you tried using the Ungreedy (U) switch?

0

精彩评论

暂无评论...
验证码 换一张
取 消