java regex matching ip address and port number as captured groups

 ̄綄美尐妖づ 提交于 2019-12-19 19:57:56

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2908740/java-regex-matching-ip-address-and-port-number-as-captured-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!