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!
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:
<?php $str = 'Text @Apple, some other stuff'; print preg_match('/@Apple(?![A-Za-z0-9]+)/', $str, $tmp) ? 'good' : 'bad'; ?> PHP:
Here's a splash of manual page to go with Kaimis reply. http://php.net/manual/de/regexp.reference.assertions.php