June 28th, 2010, 04:14 AM
An regex issue for look-around assertion
I want to match the variable in the Template , but not the function , so I write the pattern like:"\\\$[a-z-\>]+\s*(?!\()"(php code).And it matches "$this->truenam" not as I expect "$this->truename". What's wrong is it?
I dont kown how to deal with this issue, could you give me the right way?
Thanks a lot!
$template = '\$this->truename()';
$var_regexp = "\\\$[a-z-\>]+\s*(?!\()";
preg_match_all("/$var_regexp/es", $template, $matches);
June 28th, 2010, 04:24 AM
Your regexp ensures that the last thing in your match must not be followed by a '(' character. The 'e' at the end of 'truename' is followed by one of those, so it won't match.
June 28th, 2010, 04:33 AM
I'd like to match the string "$this->truename" , not "$this->truename()"(when I found there is a "(" behind the Alphabet, I wont like to match!), what regex expression should I code?
Originally Posted by ishnid
June 28th, 2010, 04:42 AM
Just remove the lookahead:
June 28th, 2010, 04:53 AM
oops , thank you verymuch!
I maybe think it too complex!
June 28th, 2010, 08:24 PM
I think your regex is right, but not fit my needs.
It should be described more explicit:
I have a template to be parsed like it as follows,
yourname : $this->name
so when I code the pattern = "\\\$[a-z-\>]+\s*(?!\()" , I expect that it matches "$this->name" and then I will replace it with something, but dont expect anything to do with "$this->truename()" (cause it seems as a function that I will deal with lately).
Can you give me some advises? Thx a lot!
June 29th, 2010, 12:32 PM
This might work for you:
I've separated it to be two a-z segments on either side of a -> (it doesn't look like you want to allow multiple >s and -s anyway). Then insist on a word boundary not followed by a (.