java正则表达式将IP地址和端口号匹配为捕获组
有人可以告诉我这个正则表达式有什么问题吗?
((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\:([0-9]{2,5})
用于匹配此:assfasfas>192.168.1.1:8080192.168.222.43:8286
我需要192.168.1.1和8080被捕获组
谢谢
could please anybody tell me what is wrong with this regexp ?
((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\\:([0-9]{2,5})
for matching this: assfasfas>192.168.1.1:8080192.168.222.43:8286
I need 192.168.1.1 and 8080 to be captured groups
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您真的,真的必须进行IP地址验证,否则我建议您简化正则表达式,因为这个野兽对于仅匹配“IP部分”来说太复杂了”和“端口部分”。我的建议是
组 1 和组 2 分别保存 IP 和端口。上面的内容已经比它需要的更复杂了,恕我直言,即使像这样简单的东西也足够了:
请注意,双反斜杠是 Java 字符串的要求,而不是正则表达式的要求,所以我将它们排除在外。
Unless you really, really have to do IP adress validation, as well, I suggest you simplify the regular expression, because this beast is far too complex for only matching "IP part" and "port part". My suggestion would be
Groups 1 and 2 will hold IP and port, respectively. And the above is already more complex that it needs to be, IMHO even something as simple as this would be enough:
Note that double backslashes are are requirement of Java strings, not of regex, so I left them out.