具有可选段的正则表达式模式无法匹配斜杠分隔的字符串
使用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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为在编写正则表达式时,您需要在
cde
后面加一个斜杠。ab/cde/
应该匹配。Because as the regex is written, you need a slash after the
cde
.ab/cde/
should match.[az-]{3,} = 3 个或更多字符
[a-z-]{3,} = 3 or more characters