我想在正则表达式中匹配一些单词,但不包括其他单词:
例:包含straat、laan、baan的所有单词
(straat|laan|baan)但不是
(overslaan|bestraat|rubaan)例如: mystraat bolaan overslaan boobaan rubaan
应匹配
mystraat bolaan boobaan
发布于 2017-01-22 21:48:05
这有点复杂,但可以通过负向回溯来完成。
尝试如下所示:
$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;发布于 2017-01-22 21:23:52
只需在您的正则表达式前面添加^,并在代码下面添加$以结束检查:
/^[straat|laan|baan]$/https://stackoverflow.com/questions/41791167
复制相似问题