I have this code which works very well
$match = false;
$string = 'The thing is S25697 and blue';
$reg_exp = '^The thing is.*(S\d).*(blue|red)';
$match = @preg_match("/$reg_exp/iu", $string);
if ($match) echo "found";
else echo "not found";
This regex gives me the following results
$string = 'The thing is S25697 and blue'; #<--$match = true
$string = 'The thing is S2w697 and green'; #<--$match = false
$string = 'The thing is S2e697 and yellow'; #<--$match = false
$string = 'The thing is S2b697 and pink'; #<--$match = false
$string = 'The thing is S256b7 and red'; #<--$match = true
Now I want to adjust the regex to get the opposite of blue or red. But the PHP code itself shall not be changed. Here is an example what I want to get as a result:
$string = 'The thing is S25697 and blue'; #<--$match = false
$string = 'The thing is S2w697 and green'; #<--$match = true
$string = 'The thing is S2e697 and yellow'; #<--$match = true
$string = 'The thing is S2b697 and pink'; #<--$match = true
$string = 'The thing is S256b7 and red'; #<--$match = false
I have found this Match everything except for specified strings and other answers. But no one work at this moment. I have try this
$match = false;
$string = 'The thing is S25697 and blue';
$reg_exp = '^The thing is.*(S\d).*(?!.*(?:blue|red))';
$match = @preg_match("/$reg_exp/iu", $string);
if ($match) echo "found";
else echo "not found";
But the result is "found"