user2965598
user2965598

Reputation: 207

Validate if input string is a number between 0-255 using regex

I am facing problem while matching input string with Regex. I want to validate input number is between 0-255 and length should be up to 3 characters long. code is working fine but when I input 000000 up to any length is shows true instead false.

Here is my code :-

String IP = "000000000000000";
        System.out.println(IP.matches("(0*(?:[0-9][0-9]?|[0-2][0-5][0-5]))"));

Upvotes: 15

Views: 55151

Answers (12)

anubhava
anubhava

Reputation: 785631

You may use this regex:

boolean valid = IP.matches("^(?:1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])$");

// since .matches expects complete match you can omit anchors
boolean valid = IP.matches("(?:1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])");

RegEx Demo

Upvotes: 13

naib khan
naib khan

Reputation: 1128

You can simplify it by thinking in four conditions that might happen

 String zeroTo255 = "((0|1)\\d{2}|2[0-4]\\d|25[0-5]|\\d{1,2})";
 String validIpPattern = zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255 + "\\." + zeroTo255;
  1. (0|1)\d{2} catches any three digit number starting with 0 or 1.
  2. 2[0-4]\d catches numbers between 200 and 249.
  3. 25[0-5] catches numbers between 250 and 255.
  4. \d{1,2} catches any one or two digit number

you can test it in https://regexr.com/

To test it here in regexr you need to remove one backslash

(\\d --> \d)

((0|1)\d{2}|2[0-4]\d|25[0-5]|\d{1,2})

Note that \d represents digits in regular expressions, same as [0-9]

Upvotes: 0

stayhydrated03
stayhydrated03

Reputation: 66

(2[0-4][0-9])|([0-1]?[0-9]?[0-9])

To match 0 to 249 specifically

Upvotes: 0

Leo
Leo

Reputation: 171

Tested this:

static String pattern = "^(([0-1]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5])\\.){3}([0-1]?[0-9]?[0-9]?|2[0-4][0-9]|25[0-5]){1}$";

It works for the following:

  • IP Addresses xxx.xxx.xxx.xxx / xx.xx.xx.xx / x.x.x.x / mix of these.
  • Leading zeros are allowed.
  • Range 0-255 / maximum 3 digts.

Upvotes: 16

BR1COP
BR1COP

Reputation: 349

Please try this

"^(((\d|0\d|00\d|\d{2}|0\d{2}|1\d{2}|2[0-4]\d|2[0-5]{2})\.){3})(\d|0\d|00\d|\d{2}|0\d{2}|1\d{2}|2[0-4]\d|2[0-5]{2})$"

It works also with leading zeroes

Upvotes: 1

John Guzenko
John Guzenko

Reputation: 1

If you want to validate ip4 with 'ip/mask', so regex looks like this:

^((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]|[1-2][0-9]|3[0-2]))$

Just ip

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

JS code to test it

function isMatchByRegexp(stringToValidate, regexp) {
var re = new RegExp(regexp);
return re.test(stringToValidate);
}

Upvotes: 0

RichArt
RichArt

Reputation: 1672

If you need leading zeros, try this:

"((\\d{1,2}|[01]\\d{1,2}|[0-2][0-4]\\d|25[0-5])\\.){3}(\\d{1,2}|[01]\\d{1,2}|[0-2][0-4]\\d|25[0-5])"

It satisfies following conditions: IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.

Maybe somebody can help with additional simplifying?

Upvotes: 0

Saurabh
Saurabh

Reputation: 7833

This will work for following pattern and ip containing initial zeros e.g: 023.45.12.56

pattern=(\\d{1,2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5]);

Upvotes: 0

mickmackusa
mickmackusa

Reputation: 47991

Using boundary tags to ensure only (0 to 255) numbers is matched, the optimized pattern that I have to offer is:

\b(?:1\d{2}|2[0-4]\d|[1-9]?\d|25[0-5])\b

Pattern Demo (in PHP/PCRE to show step count)

4010 steps when checking a list from 0 to 256.

This pattern will not match 01 or 001. (no match on one or more leading zeros)

Considerations:

  1. Use quantifiers on consecutive duplicate characters.
  2. Organize the alternatives not in sequential order (single-digit, double-digit, 100's, under-249, 250-255) but with quickest mis-matches first.
  3. Avoid non-essential capture (or non-capture) groups. (despite seeming logical to condense the "two hundreds" portion of the pattern)

Upvotes: 3

user3527035
user3527035

Reputation: 1

Complete ip inet4 match :

JS

/(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])/g.exec(myIp);

https://regex101.com/r/tU3gC3/12

Minified :

/(1?(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])/g.exec(myIp);

https://regex101.com/r/tU3gC3/13

Upvotes: 0

Etienne Lawlor
Etienne Lawlor

Reputation: 6697

boolean valid = IP.matches("(0?[0-9]{1,2}|1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])");

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272266

You can use this pattern which matches "0", "1", ... "255":

"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"

Demo on Ideone

Upvotes: 8

Related Questions