Search code examples
regexpreg-replacepreg-replace-callback

Applying NOT restriction to a regex pattern


I use preg_replace_callback with the pattern of

preg_replace_callback('/^(word1 .*),(.*)\./i',
function($m){
processing $m[1] and $m[2]
}, $str);

to match a sentence start with word1. How can I modify the pattern to make a restriction for the second word in the pattern?

The first match above should start with word1 as long as word2,word3,etc are NOT the second word.

I do not want to alter $m[1] and $m[2], just to match when word2,word2,etc are the second word in the string.

It should match,

word1 something, and more.

but not

word1 word2 something, and more.
word1 word3 something, and more.

Solution

  • If you want to match lines which begin with word1, but word2 and word3 do not occur anywhere else in the line, then you can try using a negative lookahead assertion:

    ^word1(?!.*\b(?:word2|word3)\b).*$
    

    Demo

    The (?!.*\b(?:word2|word3)\b) term asserts that we do not see word2 or word3 as standalone words anywhere else in the remainder of the line.

    Here is the actual PHP code:

    preg_replace_callback('/^(word1(?!.*\b(?:word2|word3)\b).*),(.*)\./i',
        function($m) {
            # processing $m[1] and $m[2]
        }, $str);
    

    Not much really changes in the above code, except that I inserted a negative lookahead to make sure that word2 and word3 do not occur.