正则表达式匹配 url 权限部分
我需要匹配以下字符串的这些部分:
(user)@(hostname):(port)
用户和端口可以选择匹配。 首先我用这个正则表达式来管理它:
(?:([^@]*)@)?([^\:]+)(?:\:(\d+))?
This matches for foo@bar:80
foo
bar
80
但是当涉及到像 foo@[2001:0db8:85a3:08d3:1319:8a2e 这样的 IPv6 主机时:0370:7344]:80
,前面的正则表达式将无法按预期工作:
foo
[2001
0
所以现在我正在考虑一个正则表达式,它也可以匹配方括号内带有冒号但没有方括号的主机。 :) 我已经使用以下正则表达式完成了此操作:
(?:([^@]*)@)(?:\[(.+)\]|([^:]+))(?:\:(\d+))?
foo
2001:0db8:85a3:08d3:1319:8a2e:0370:7344
<empty>
80
但是..这很丑陋,因为 2
或 3
将为空。 有什么方法可以将其合并到只有一个反向引用吗?
我正在使用 boost::regex,据我所知,它使用 perl 的正则表达式引擎。
谢谢并致以
诚挚的问候
I need to match these parts of the following string:
(user)@(hostname):(port)
User and port can optionally be matched.
First I managed it with this regular expression:
(?:([^@]*)@)?([^\:]+)(?:\:(\d+))?
This matches for foo@bar:80
foo
bar
80
But when it comes to a IPv6 host like foo@[2001:0db8:85a3:08d3:1319:8a2e:0370:7344]:80
, the preceding regex won't work as expected:
foo
[2001
0
So now I'm pondering about a regular expression which can also match square bracket enclosed hosts with colons, but without square brackets. :)
I've done that with the following regex:
(?:([^@]*)@)(?:\[(.+)\]|([^:]+))(?:\:(\d+))?
foo
2001:0db8:85a3:08d3:1319:8a2e:0370:7344
<empty>
80
But.. this is ugly, because either 2
or 3
will be empty.
Is there any way to combine this to only one backreference?
I'm using boost::regex, which uses perl's regex engine as far as I know.
Thanks and regards
reeaal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但如果它是 IPv6 地址,则必须去掉
[]
。不过应该相当微不足道。您还可以在之前和之后使用可选的
[
和]
来完成此操作,然后环视断言...但这真的很难看;如果你只是 KISS 并使用上面的内容,你的程序员同事会感谢你,但这里有一个选项:But you'll have to strip off the
[]
if it's an IPv6 addr. Should be fairly trivial though.You could also do it with optional
[
and]
before and after, and then lookaround assertions... but that's REALLY ugly; your fellow programmers will thank you if you just KISS and use the above, but here's the option: