[英]regex: How to match these words except those words?
除了其他一些詞,我想匹配正則表達式中的一些詞:
例如:所有包含straat,laan,baan的單詞
(straat|laan|baan)
但不是
(overslaan|bestraat|rubaan)
例如:mystraat bolaan overslaan boobaan rubaan
應該匹配
mystraat bolaan boobaan
這有點復雜,但是可以通過消極的掩飾來完成。
嘗試這樣的事情:
$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;
只需在正則表達式和$之前添加^,以結束下面的代碼:
/^[straat|laan|baan]$/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.