我正在尝试匹配一系列跨越2行的单词。
假设我有以下文本:
this is a test
another line
使用preg_match:的我的正则表达式模式
/test.*another/si
此处测试:http://www.phpliveregex.com/p/2zj
PHP模式修改器:http://php.net/manual/en/reference.pcre.pattern.modifiers.php
我读到的所有内容都指向使用"s"修饰符来启用"."字符以匹配新行,但我无法实现这一点。有什么想法吗?
您的正则表达式是正确的,在我的本地机器上运行良好:
$input_line = "this is a test
another line";
preg_match("/test.*another/si", $input_line, $output_array);
var_dump($output_array);
它产生以下输出:
array(1) {
[0]=>
string(13) "test
another"
}
所以我的猜测是phplivergex.com工作不正常,给你的结果是错误的。
在正则表达式中放入修饰符:
/(?s)test.*another/i
是的,s
修饰符(也称为dotall修饰符)强制点.
也匹配换行符。
您的正则表达式使用正确,这似乎对我有效。
$text = <<<DATA
this is a test
another line
DATA;
preg_match('/test.*another/si', $text, $match);
echo $match[0];
请参阅此处的工作demo
。
输出
test
another