Php regex:除了那些单词,如何匹配这些单词?
我想匹配正则表达式中的一些单词,但其他单词除外: 例:所有包含straat、laan、baan的单词Php regex:除了那些单词,如何匹配这些单词?,php,regex,regex-negation,Php,Regex,Regex Negation,我想匹配正则表达式中的一些单词,但其他单词除外: 例:所有包含straat、laan、baan的单词 (straat|laan|baan) 但不是 (overslaan|bestraat|rubaan) ex:mystraat bolaan overslaan boobaan rubaan 应该匹配 mystraat bolaan boobaan只需在正则表达式前面添加^ /^[straat|laan|baan]$/ 这有点复杂,但可以通过反向查找来完成 试着这样做: $goodStrin
(straat|laan|baan)
但不是
(overslaan|bestraat|rubaan)
ex:mystraat bolaan overslaan boobaan rubaan
应该匹配
mystraat bolaan boobaan只需在正则表达式前面添加^
/^[straat|laan|baan]$/
这有点复杂,但可以通过反向查找来完成 试着这样做:
$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;
$goodString=“coolbaan”;
$badString=“rubaan”;
$stringToTest=$goodString;
$regexpatern='/(.*)(?这已经回答了很多次。您可以使用前瞻断言。查看是否要匹配整个单词,而不仅仅是部分,类似这样的操作应该可以: