Mod 重写正则表达式,仅在前一个子集匹配时才匹配
我正在尝试制作一个我认为与 mod_rewrite 一起使用的简单正则表达式。
我尝试过各种表达方式,其中许多我认为很有希望,但最终都因为这样或那样的原因失败了。一旦我添加开始/结束字符串分隔符,它们似乎也会失败。
例如, ^user/(\d{1,10})(?=/)$
是我尝试过的一个,但除此之外,它似乎对尾部斜杠进行了分组,我只想对数字进行分组。我认为我需要使用积极的回顾,但我遇到了困难,因为它是在回顾一个群体。
我想要匹配的是 1) 以“user/”开头和 2) 可能以 (\d{1,10})/
结尾的字符串(1 到 10 位数字后跟一个斜杠)
应该匹配:
user/
user/123/
user/1234567890/
不应该匹配:
user
user//
user/-4/
user/35.5/
user/123
user/123//
user/123/5/
user/12345678901/
编辑:对格式感到抱歉;我不明白如何通过此降价格式化任何内容。这些示例前面有 4 个空格,我认为它们应该构成一个代码块,但显然我的想法是错误的。
I am trying to make what I think is a simple regex for use with mod_rewrite.
I've tried various expressions, many of which I thought were promising, but all of which ultimately failed for one reason or another. They all also seem to fail once I add start/end string delimiters.
For example, ^user/(\d{1,10})(?=/)$
was one I tried, but among other things, it seems to group the trailing slash, and I only want to group the digits. I think I need to use a positive lookbehind, but I'm having difficulty because it's looking behind at a group.
What I am trying to match is strings that 1) begin with "user/" and 2) possibly end with (\d{1,10})/
(1 to 10 digits followed by a single slash)
Should Match:
user/
user/123/
user/1234567890/
Should not match:
user
user//
user/-4/
user/35.5/
user/123
user/123//
user/123/5/
user/12345678901/
Edit: Sorry about the formatting; I do not understand how to format anything via this markdown. Those examples are preceded by 4 spaces which I thought should make a code block, but obviously I thought wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
^user/(?:([0-9]{1,10})/)?$
应该可以正常工作。^user/(?:([0-9]{1,10})/)?$
should work just fine.这是:
^user(?=/)(/\d{1,10})?/$
编辑:如果要对数字进行分组,^user(?=/)(? :/(\d{1,10}))?/$
This:
^user(?=/)(/\d{1,10})?/$
Edit: if you want to group digits,^user(?=/)(?:/(\d{1,10}))?/$