#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    An regex issue for look-around assertion


    Hi everyone!
    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?

    PHP Code:
    $template '\$this->truename()';
    $var_regexp "\\\$[a-z-\>]+\s*(?!\()";
    preg_match_all("/$var_regexp/es"$template$matches);
    var_dump($matches);exit; 
    Thanks a lot!
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0
    Originally Posted by ishnid
    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.
    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?
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Just remove the lookahead:
    Code:
    \\\$[a-z->]+
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0
    oops , thank you verymuch!
    I maybe think it too complex!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0
    to ishnid:
    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,
    <html>
    ...
    $this->truename()
    ...
    yourname : $this->name
    <html>

    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!
  12. #7
  13. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    This might work for you:
    Code:
    \\\$[a-z]+->[a-z]+\b(?!\()
    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 (.

IMN logo majestic logo threadwatch logo seochat tools logo