Search code examples
phpregexpreg-match

Getting someone's name using regex lookahead


I'm trying to use preg_match to get someone's name, but I don't want it to get their name if they say something like "my name's NOT so and so...", but I'm having trouble matching it:

$string = "my name is james";
preg_match("~my name is (?>!\bnot\b)[a-z]+~", $string, $match);
print_r($match);

Since my original string does not have the word "not" in it, it should've matched the string, but it did not. What am I doing wrong?

Result:

Array ( )

Expected Result:

Array ( my name is james )


Solution

  • negative lookahead doesn't use >, does it? (look behind does use <):

    /(?!\bnot\b)[a-z]+/