Joe
Joe

Reputation: 1772

php regex preg_match_all issue

I'm trying to figure out a problem I'm having with regex.

I'm using this regex with preg_match_all on a large multi line string:

 /(\{(if|while|function|loop|\$|#)(.+)\})/

It currently works to match all text that starts with { and ends with } such as {$test} or {$function="test()"}

However, if one line in the string contains two matching blocks, the regex returns the whole line such as:

{$value.url}" class="link">{$value.title}

I can't figure out how to make the regex not do a 'greedy' match with (.+). The reason why I have (.+) is because there could be any character/number/underscore/period/quote/space in between the two brackets {}.

Can someone help me out?

Upvotes: 0

Views: 165

Answers (2)

stema
stema

Reputation: 92976

You can make the .+ ungreedy by adding a question mark like this .+?

Upvotes: 1

Ted Kulp
Ted Kulp

Reputation: 1433

Try matching for everything except for the } and then the }.

/(\{(if|while|function|loop|\$|#)([^\}]+)\})/

Upvotes: 1

Related Questions