7

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 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

3 Comments

But compiler says: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
It was because of I declared it as String, sorry
As I said, Java strings require the backslash to be escaped. So a \d in regex will be a \\d in a Java string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.