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?
You can make the .+
ungreedy by adding a question mark like this .+?
Try matching for everything except for the } and then the }.
/(\{(if|while|function|loop|\$|#)([^\}]+)\})/
精彩评论