具有可选段的正则表达式模式无法匹配斜杠分隔的字符串

发布于 2024-12-03 21:40:42 字数 583 浏览 0 评论 0原文

使用preg_match时,为什么#^(([az]{2})/)?(([az\-]{3,})/(([az\-]{ 3,}))?)?$#i 匹配 ab/cde/fgh 且不匹配 ab/cde

我的意思是:

preg_match_all('#^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i','ab/cde/fgh',$match)

$match =  Array
(
    [0] => ab/cde/fgd
    [1] => ab/
    [2] => ab
    [3] => cde/fgd
    [4] => cde
    [5] => fgd
    [6] => fgd
)

preg_match_all('#^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i','ab/cde',$match)
$match = Array ()

When using preg_match, why #^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i match ab/cde/fgh and do not match ab/cde?

I mean:

preg_match_all('#^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i','ab/cde/fgh',$match)

$match =  Array
(
    [0] => ab/cde/fgd
    [1] => ab/
    [2] => ab
    [3] => cde/fgd
    [4] => cde
    [5] => fgd
    [6] => fgd
)

and

preg_match_all('#^(([a-z]{2})/)?(([a-z\-]{3,})/(([a-z\-]{3,}))?)?$#i','ab/cde',$match)
$match = Array ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风吹短裙飘 2024-12-10 21:40:42

因为在编写正则表达式时,您需要在 cde 后面加一个斜杠。 ab/cde/ 应该匹配。

Because as the regex is written, you need a slash after the cde. ab/cde/ should match.

蓝戈者 2024-12-10 21:40:42

[az-]{3,} = 3 个或更多字符

[a-z-]{3,} = 3 or more characters

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