nelac123 nelac123 - 7 days ago 5
Java Question

parsing numerical address

I have been trying to parse a numerical address from a string using regex.

So far, I have been able to successfully get the numerical address (partially) 63.88.73.26:80 from the string http://63.88.73.26:80/. However I have been trying to skip over the :80/, and have had no luck.

What I have tried so far is:

Pattern.compile("[0-999].*[0-999][\\p{Digit}]", Pattern.DOTALL);


however does still includes :80

I dont know what I am missing here, I have tried to check for \p{Digit} at the end, but that doesn't do much either

Thanks for your time!

Answer

You are looking for a positive look ahead (?=...). This will match only if it is followed by a specific expression, the one in the positive look ahead's parenthesis. In it's simplest form you could have

[0-9\.]+(?=:[0-9]{0,4})

Though you may want to change the [0-9\.]+ part (match 1 or more digit or full stop) with something more complete to check that you have a properly formed address

Check out regexr.com where you can fiddle your expression to your heart's content until it works...