如何让这个 URL 验证正则表达式不那么贪婪?
因此,我有以下正则表达式:
https?://(www\.)?flickr\.com/photos/(.+)/?
要与以下 URL 匹配:
http://www.flickr.com/photos/username/
如何阻止最后的正斜杠 (/
) 包含在用户名子模式 (.+)< 中/代码>?
我尝试过:
https?://(www\.)?flickr\.com/photos/(.+?)/?
但是它只匹配用户名的第一个字母。
So I have the following regular expression:
https?://(www\.)?flickr\.com/photos/(.+)/?
To match against the following URL:
http://www.flickr.com/photos/username/
How can I stop the final forward slash (/
) from being included in the username sub-pattern (.+)
?
I have tried:
https?://(www\.)?flickr\.com/photos/(.+?)/?
But then it only matches the first letter of the username.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将
?:
添加到第一组,因此它不会捕获,然后使用[^/]
代替最后一场比赛中的点。这可以确保捕获“photos/”和下一个“/”之间的所有内容。如果您需要捕获第一个
www
只需使用以下命令:I added
?:
to the first group so it's not capturing, then used[^/]
instead of the dot in the last match. This assures you that everything between "photos/" and the very next "/" is captured.If you need to capture the first
www
just use this:您需要确保它与正斜杠不匹配:
您还可以使正则表达式变得懒惰(我猜您正在使用
(.+?)
语法执行此操作),但是上面的会工作得很好You need to make sure it doesn't match the forward slash:
You could also make the regex lazy (which is what I guess you were doing with the
(.+?)
syntax), but the above will work just fine将
(.+)
更改为([^/]+)
。这将匹配,直到遇到/
,因此您可能还想在类中添加一些其他内容。Change
(.+)
to([^/]+)
. This will match until it encounters a/
, so you might want to throw some other stuff in the class too.通常有两种方法可以做到这一点:
附加问号,使匹配非贪婪。
.*
将尽可能匹配,.*?
将尽可能少匹配。排除接下来要匹配的字符。如果您想在
/
处停止,请使用[^/]*
。There are generally two ways to do this:
Append a question mark, to make the matching non-greedy.
.*
will match as much as possible,.*?
will match as little as possible.Exclude the character you want to match next. If you want to stop on
/
, use[^/]*
.如果您知道尾部会有斜杠,请取出最后的
?
。If you know there will be a trailing slash, take out the final
?
.