java regex matching ip address and port number as captured groups

后端 未结 1 1937
悲哀的现实
悲哀的现实 2020-12-19 08:10

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]?))         


        
相关标签:
1条回答
  • 2020-12-19 08:31

    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.

    0 讨论(0)
提交回复
热议问题