I want to match some words in regex except some others:
ex: all words that contains straat, laan, baan
(straat|laan|baan)
But not
(overslaan|bestraat|rubaan)
ex: mystraat bolaan overslaan boobaan rubaan
should match
mystraat bolaan boobaan
I want to match some words in regex except some others:
ex: all words that contains straat, laan, baan
(straat|laan|baan)
But not
(overslaan|bestraat|rubaan)
ex: mystraat bolaan overslaan boobaan rubaan
should match
mystraat bolaan boobaan
That's a bit complex but can be done with a negative lookbehind.
Try something like this:
$goodString = "coolbaan";
$badString = "rubaan";
$stringToTest = $goodString;
$regexPattern = '/(.*?)((?<!overs|ru|be)(straat|laan|baan))/';
preg_match($regexPattern, $stringToTest, $matches);
if ($matches) {
// $matches[1] will be the prefix - e.g. ru
// $matches[2] will be the suffix e.g. baan
// $result will be 'rubaan'
$result = "{$matches[1]}{$matches[2]}";
} else {
$result = 'No Match!';
}
echo $result;
just add ^ in front of your regex and $ to end check below code:
/^[straat|laan|baan]$/