0

I am trying to validate whether a number with decimals in between is within the range of 0-255 which allows leading zeroes.

So far I have come up with the regex

^0*([1-9][\.\d]|250)*

and the example that I have tried it on is 63.32.32.250 however it only grabs the 63, and not the rest of the string.

I thought [\.\d] would include all the periods, however it isn't the case.

What am I doing wrong here? Thanks

beronsus
  • 5
  • 1
  • 2
  • are you trying to make a regex for IPV4 addresses? – valepu Dec 02 '16 at 14:47
  • Possible duplicate of [Regular expression for IP Address Validation](http://stackoverflow.com/questions/10006459/regular-expression-for-ip-address-validation) – AxelH Dec 02 '16 at 14:47
  • See [*Validate IPv4 address in Java*](http://stackoverflow.com/questions/5667371/validate-ipv4-address-in-java). – Wiktor Stribiżew Dec 02 '16 at 14:47
  • Nope, I'm not really trying to make a regex for IPV4 addresses, just for any random numerical address with 3 decimal points with 4 numbers. I've taken a look at the suggested links, and those regex seem particularly long. Is there a more elegant solution perhaps? – beronsus Dec 02 '16 at 14:50
  • A numerical adress separated by `.` with values between 0 and 255. Well, this seems pretty close to IPv4 ... you should use the same logic you know. And check SO, there is planty of regex, a range like this is not simple is Regex. But you can split the String and check the numerical value range – AxelH Dec 02 '16 at 14:51
  • The part in square brackets is a character class. ```[\.\d]``` is one period or one digit. Ignoring the leading 0 thing completely: You're matching 1-9 followed by a period OR another digit. "1." would match. "12" would as well, but wouldn't include the period. "122" wouldn't match at all. – Benjamin Podszun Dec 02 '16 at 14:51
  • More elegant solution is not to use a regex. Split with a dot, parse the numbers, check. Anyway, as Java regex does not support pattern recursion/subroutine calls, you cannot shorten those regexps. – Wiktor Stribiżew Dec 02 '16 at 14:52
  • I see... alright. Thanks for the suggestions everyone! – beronsus Dec 02 '16 at 14:54

4 Answers4

2

Using a regex for this is not a good idea, you can use InetAddressValidator like this:

InetAddressValidator.getInstance().isValid(YOUR_IP_HERE)

Btw, you can use isValidInet4Address method as well.

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
1

Federico Piazza's answer is better but if you want to do it manually I would think about using the split function and checking using ><, e.g...

String[] numbers = "63.32.32.250".split("\\.");
for(String number: numbers){
   -> parse number string to an int
   -> check its in right range just with ><=
}

Hope that helps

Lomas
  • 34
  • 4
0

Works w/ Flaw

To validate IPv4 addresses you would do something like

^((?:\d{1,3}\.){3}\d{1,3})$

Regular expression visualization

Debuggex Demo

But this has the vital flaw of not checking if the number is between 1-255

Works

^(?:(?: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]?)$

Regular expression visualization

Debuggex Demo

abc123
  • 17,855
  • 7
  • 52
  • 82
0

For one group of digits:

^0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

For four groups, repeat:

^0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.0*([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

This will capture four groups.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24