[RESOLVED] Regular expression help
Results 1 to 3 of 3

Thread: [RESOLVED] Regular expression help

  1. #1
    Junior Member
    Join Date
    Feb 2002
    Posts
    20

    resolved [RESOLVED] Regular expression help

    Using php I would like to detect something in a string depending on whether the part immediately after the matched part does not contain certain characters. I think I may need to use a lookahead negative assertion as explained here but it is not working as I would expect so not sure. So for example:

    Code:
    $string = 'test aadfadf random text kalkdfjakdf>apple peach orange';
    I want an expression that will detect everything up to the > so long as apple does not immediately follow the >

    I tried

    Code:
    if(preg_match("/test(.*)>(?!apple))/i",$string)){
    echo 'true since apple does not follow > in string';
    }else{
    echo 'false since apple follows > in string';
    }
    When the string contains apple after the > it returns false as I expect and need but when I change the string to have peach after the > instead of apple it still returns false and I need it to return true. Any ideas?

  2. #2
    Senior Member
    Join Date
    Jul 2007
    Posts
    3,658
    Your pattern can't be parsed due to unmatched number of parenthesis. But if you fix that, it will indeed match / not match depending on if apple follows > or not.

  3. #3
    Junior Member
    Join Date
    Feb 2002
    Posts
    20
    Thanks for pointing out the error, working fine now.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •