Forum Moderators: coopster

Message Too Old, No Replies

regex, problem with backreference in pattern with preg match all

         

ahmedhelmy007

8:55 pm on May 18, 2011 (gmt 0)

10+ Year Member



i wonder what is the problem with the backreference here:

preg_match_all('/__((\'|")([^\1]+)\1/', "__('match this') . 'not this'", $matches);


it is expected to match the string between __('') but actually it returns:


match this') . 'not this

coopster

2:46 pm on May 24, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks like it is getting greedy. Try adding a non-greedy modifier to your expression. Also, you may want to escape the parenthesis. It may not be necessary but since it is meant to capture subpatterns I typically make a habit of escaping them when they are literal values.