Reputation: 87
I can't seem to get the proper RegEx for validating an IP address, including support for a wildcard char (*), which can occur only at the end. For example:
Valid
1.2.*
1.2.3.*
1.2.3.4
Not Valid
1
1.2
1.2*
1.2.3
1.2.3*
1.*.3.4
I've come close (and found a few similar questions/answers here), but can't get all of the scenarios to pass/fail. Can anyone help me out? BTW - validating octets are 0-255 isn't necessary, but would be cool.
Upvotes: 2
Views: 5298
Reputation: 10250
All on one line:
^(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)\.(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)\.(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)\.(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)))))
Broken down:
^(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)\. # Wildcard, or 1st octet plus...
(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)\. # Wildcard, or 2nd octet plus...
(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)\. # Wildcard, or 3rd octet plus...
(?:\*|(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?) # Wildcard, or 4th octet
))))$ # Close up shop
Alternatively:
^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)\.){0,3}(?:(?:25[0-5]|2[0-4]\d|1\d{2}|\d\d?)|\*)$
Upvotes: 1
Reputation: 14119
Nice and short
^((\d+\.){3}\d+|(\d+\.){1,3}\*)$
With the regex modifier that lets ^ and $ match at line start/end
Upvotes: -1
Reputation: 20398
^(?:(?:[0-1]\d\d|2[0-4]\d|25[0-5])|(?:\d{1,2}))(?:(?:(?:\.(?:(?:[0-1]\d\d|2[0-4]\d|25[0-5])|(?:\d{1,2})))){3}|(?:\.(?:(?:[0-1]\d\d|2[0-4]\d|25[0-5])|(?:\d{1,2}))){0,2}\.\*)$
Here's a short ruby script to show construction and validate results:
#!/usr/bin/env ruby
octet2 = /(?:\d{1,2})/
octet3 = /(?:[0-1]\d\d|2[0-4]\d|25[0-5])/
octet = /(?:#{octet3}|#{octet2})/
dot_octet = /(?:\.#{octet})/
trailing_wild_ip = /^#{octet}(?:(?:#{dot_octet}){3}|#{dot_octet}{0,2}\.\*)$/
%w{
1.2.*
1.2.3.*
1.2.3.4
1
1.2
1.2*
1.2.3
1.2.3*
1.*.3.4
}.
map {|ip| [ip, ip.match(trailing_wild_ip) ? 'valid' : 'invalid' ] }.
each {|ip,match| puts "#{ip} => #{match}" }
# output:
1.2.* => valid
1.2.3.* => valid
1.2.3.4 => valid
1 => invalid
1.2 => invalid
1.2* => invalid
1.2.3 => invalid
1.2.3* => invalid
1.*.3.4 => invalid
Upvotes: -1
Reputation: 43683
Regex:
\b((?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])|(?\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){0,3}\*))\b
If you are validating entire string to be a IP address, then replace \b
with ^
(beginning) and $
(end), otherwise it will be looking for match within string.
Upvotes: 0
Reputation: 225144
Match up to 3 parts and then a wildcard, or a regular IP:
^((\d{1,3}\.){1,3}\*|(\d{1,3}\.){3}\d{1,3})$
Or, if you want to also validate the numbers, change the \d{1,3}
s to (1?\d?\d|2[0-4]\d|25[0-5])
.
Upvotes: 0
Reputation: 13460
something 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]?))|(((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]?)\.){2}\*)|(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){1}\*))$
second edition:
^((((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]?))|(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){1,3}\*))$
Upvotes: 2