java正则表达式将IP地址和端口号匹配为捕获组

发布于 2024-09-03 10:23:28 字数 268 浏览 0 评论 0原文

有人可以告诉我这个正则表达式有什么问题吗?

((?:(?: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 技术交流群。

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

发布评论

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

评论(1

不再见 2024-09-10 10:23:28

除非您真的,真的必须进行IP地址验证,否则我建议您简化正则表达式,因为这个野兽对于仅匹配“IP部分”来说太复杂了”和“端口部分”。我的建议是

(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})

组 1 和组 2 分别保存 IP 和端口。上面的内容已经比它需要的更复杂了,恕我直言,即使像这样简单的东西也足够了:

(\d+\.\d+\.\d+\.\d+):(\d+)

请注意,双反斜杠是 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

(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})

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:

(\d+\.\d+\.\d+\.\d+):(\d+)

Note that double backslashes are are requirement of Java strings, not of regex, so I left them out.

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