正则表达式问题 - 缺少匹配项

发布于 2024-08-04 02:40:22 字数 218 浏览 2 评论 0原文

这是一个简短的正则表达式示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-08-11 02:40:22

末尾部分:

-\s*(\d{2})?$~u

意味着在匹配项和字符串末尾之间只能有空格和/或可选的两位数字。这意味着 00- 无法匹配,因为它和字符串末尾之间还有其他内容。

如果删除 $,它应该按您的预期工作。

The portion at the end:

-\s*(\d{2})?$~u

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文