PHP regexp not working


PHP regexp not working

我想在下面的例子中匹配"TEST:":

$text = "TEST:
This is a test.";
preg_match_all("/^([A-Z]+)':$/m", $text, $matches);

但是$matches是空的

$text = "TEST:
This is a test.";
preg_match_all("/^([A-Z]+).*':*$/m", $text, $matches);
输出:

Array
(
    [0] => Array
        (
            [0] => TEST:
            [1] => This is a test.
        )
    [1] => Array
        (
            [0] => TEST
            [1] => T
        )
)

但是我只想让它匹配"TEST:"。

我在这里做错了什么?它似乎与模式中的冒号有问题,但如果我不转义它也不起作用。

谢谢你的帮助!

实际上,当你使用$来表示行结束时,要非常小心字符串的构建位置:在windows上,行尾是'r'n,而在unix上只有'n$ end分隔符只识别'n作为行结束符

$text = "TEST:
This is a test.";
$text = str_replace("'r", "", $text);
preg_match_all("/^([A-Z]+)':$/m", $text, $matches);

可以正常工作