Java regex matches ip address and port number as captured groups

might like someone tell me what is wrong with this regex?

((?:(?: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 comparison: assfasfas> 192.168.1.1:8080192.168.222.43:8286

I need 192.168.1.1 and 8080 to capture groups

thanks

+4
source share
1 answer

If you really donโ€™t need IP address validation, I suggest you simplify the regular expression because this beast is too complex to match only the โ€œ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 contain IP and port, respectively. And above it is already more difficult what it should be, IMHO even something simple, as it would be enough:

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

Note that double backslashes are the requirements of Java strings, not regular expressions, so I left them out.

+11
source

Source: https://habr.com/ru/post/1310911/


All Articles