我有以下表达式:
$exp = "/^(?!.*?that).*$/";
这里需要匹配任何不包含“that”的行。
我有以下三个句子:
$str = array(
"I like this sentence.", #line1
"I like that sentence.", #line2
"I link THAT sentence." #line3
);
匹配区分大小写,因此只有第1行和第3行匹配。目前为止还不错。
然而,我希望它是不区分大小写的,这样它只匹配第1行。我尝试使用内联修饰符"(?-i ... )":
$exp = "/^(?!.*?(?i:that)).*$/";
作为一个标志,即"/.../i":
$exp = "/^(?!.*?that).*$/i";
但是没有任何效果。
我使用以下循环运行搜索:
foreach($str as $s) {
preg_match_all($exp, $s, $matches);
var_dump($matches);
}
输出结果:
array (size=1)
0 =>
array (size=1)
0 => string 'I like this sentence.' (length=21)
array (size=1)
0 =>
array (size=0)
empty
array (size=1)
0 =>
array (size=1)
0 => string 'I link THAT sentence.' (length=21)
这里提供一个在线演示:https://regex101.com/r/bs9rzF/1
我很感激任何有关如何使我的正则表达式不区分大小写的提示。
编辑: 我错误地使用了"?-i"而不是"?i",正如一些贡献者正确指出的那样。现已更正。
$exp = "/^(?!.*?that).*$/i";
is correct and should work. However(?-i:...
switches off the i modifier. To switch it on:(?i:...
- Casimir et Hippolyte$exp = "/^(?!.*?that).*$/i";
应该能够工作。 应该能够工作。 - revo"/^(?!.*?that).*$/ui"
。感谢您的快速回答! - msoutopico