tags:

views:

58

answers:

4

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

+2  A: 

This should do the trick:

{[^}]*(<|>).*}
qbi
I up-voted, but you really need to make the .* match lazy or replace it with another [^}]* to avoid problems with greedyness if there will ever be multiple sets on the same line.
Cags
You'd need the ungreedy modifier, otherwise the first character class will gobble up everything up until the first `}` character...
ircmaxell
just added a ? after .* to make it greedy and it works correctly with multiple matches on the same line.
Brent Baisley
You'd need to add a ? after both `*` characters...
ircmaxell
A: 
{[^}]*?(<|>)[^{]*?}

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

Mike Caron
A: 

Have you tried using the Ungreedy (U) switch?

Mark Baker
+1  A: 

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

ircmaxell