PHP正则表达式失败,在Regex 101上取得成功

时间:2013-03-06 17:10:05

标签: php regex

被这一个O_o ......

困扰
if(preg_match("(\d{1,2})\:(\d{2})", "5:00", $matches) == 1) echo "works";
else echo "don't work";

将回显

don't work

但是Regex 101上的正则表达式似乎运行正常:http://regex101.com/r/mW1dN9

我需要全球旗帜吗?

2 个答案:

答案 0 :(得分:3)

你的正则表达式需要一些分隔符。

if(preg_match("#(\d{1,2})\:(\d{2})#", "5:00", $matches) == 1) echo "works";
else echo "don't work";

答案 1 :(得分:2)

您需要将正则表达式放在分隔符中:

if(preg_match("/(\d{1,2}):(\d{2})/", "5:00", $matches) == 1) echo "works";
else echo "don't work";

此外,您无需转义:,但无论如何都可以。