预匹配问题

发布于 2024-08-31 04:01:08 字数 228 浏览 4 评论 0原文

如何在 preg_match 中添加字符 -

preg_match('#^(\w+/){0,2}\w+\.\w+$#', $string)

但必须在最后一个之前。字符串内。我已经尝试了这里我所知道的一切。我知道 - 需要转义。所以我尝试在各个地方逃避它,但它不起作用:(

argggg

How do I add the character - within the preg_match?

preg_match('#^(\w+/){0,2}\w+\.\w+$#', $string)

But it must be before the last . within the string. I've tried just about everything I know here. I know that the - needs to be escaped. So I tried to escape it in various places, but it's not working :(

argggg

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

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

发布评论

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

评论(1

晨敛清荷 2024-09-07 04:01:08

您对需要转义破折号的了解是不完整的。

preg_match('#^(\w+/){0,2}\w+-\.\w+$#', $string)

它需要在字符类中进行转义,因为它在那里有特殊的含义,但是它在正则表达式的其余部分没有特殊含义,因此不需要在这里转义。

Your knowlegde that the dash needs to be escaped is incomplete.

preg_match('#^(\w+/){0,2}\w+-\.\w+$#', $string)

It needs to be escaped in character classes, because it has a special meaning there, but it has no special meaning in the rest of the regex, so it needs no escaping here.

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