Regular Expression "Not Followed By" Help

Discussion in 'PHP' started by MrQuidam, Apr 18, 2010.

  1. #1
    Hey,

    I'm currently using preg_match(), and I've ran into a regular expression question.

    I want to search a chunk of text for something like @Apple. I only want to return true though if it's @Apple.

    So, "Text @Apple, some other stuff." returns true.

    But, "Text @Apple123 some other stuff." doesn't.

    Can any tell me what the regular expression is to check that something ISN'T followed by A-Za-z0-9? Any other punctuation is fine. I just want to make sure it is only finding @Apple, not any other alteration of it.

    Thanks in advance for any help!
     
    MrQuidam, Apr 18, 2010 IP
  2. zerophean

    zerophean Peon

    Messages:
    91
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this one

    
    
    $txt='Text @Apple, some other stuff';
    $re1='.*?';
    $re2='(@)';
    $re3='(Apple)';	# Word 1
    
    $re=$re1.$re2.$re3;
    if ($txt =~ m/$re/is)
    {
        $c1=$1;
        $word1=$2;
        print "($c1) ($word1) \n";
    }
    
    
    PHP:
     
    zerophean, Apr 19, 2010 IP
  3. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    
    $str = 'Text @Apple, some other stuff';
    
    print preg_match('/@Apple(?![A-Za-z0-9]+)/', $str, $tmp) ? 'good' : 'bad';
    ?>
    
    PHP:
     
    Kaimi, Apr 19, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    joebert, Apr 20, 2010 IP