正则表达式问题 - 缺少匹配项
这是一个简短的正则表达式示例:
preg_match_all('~(\s+|/)(\d{2})?\s*–\s*(\d{2})?$~u', 'i love regex 00– / 03–08', $matches);
print_r($matches);
正则表达式仅匹配“03–08”,但我的意图是也匹配“00–”。问题是什么?有人可以解释一下吗?
Here's a short regex example:
preg_match_all('~(\s+|/)(\d{2})?\s*–\s*(\d{2})?$~u', 'i love regex 00– / 03–08', $matches);
print_r($matches);
The regex only matches '03–08', but my intention was matching '00–' as well. What is the problem? Anyone could explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
末尾部分:
意味着在匹配项和字符串末尾之间只能有空格和/或可选的两位数字。这意味着
00-
无法匹配,因为它和字符串末尾之间还有其他内容。如果删除
$
,它应该按您的预期工作。The portion at the end:
Means that you can only have spaces and/or optionally two digits between your match and the end of the string. This means
00-
can't match since there's other stuff between it and the end of the string.If you remove the
$
, it should work as you intend.