Brent Baisley
Brent Baisley

Reputation: 12721

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

Upvotes: 0

Views: 1356

Answers (4)

ircmaxell
ircmaxell

Reputation: 165191

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

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212412

Have you tried using the Ungreedy (U) switch?

Upvotes: 0

Mike Caron
Mike Caron

Reputation: 14561

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

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

Upvotes: 0

qbi
qbi

Reputation: 2144

This should do the trick:

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

Upvotes: 4

Related Questions