开发者

php regex preg_match_all issue

开发者 https://www.devze.com 2023-03-23 23:45 出处:网络
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:

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|\$|#)([^\}]+)\})/

0

精彩评论

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