2

I am trying to have my regex match the following:

169.254.0.0-169.254.254.255

Could anyone please help how can I achieve this.

so far I have this:

169\.254\.([1-9]{1,2}|[1-9]{1,2}[1-4])

but it would also pick up 169.254.255.1 which should not be one of the matches.

Please help!

thanks

SilentGhost
  • 264,945
  • 58
  • 291
  • 279
  • Possible duplicate of [Validating IPv4 addresses with regexp](https://stackoverflow.com/q/5284147/608639) – jww Nov 12 '18 at 09:17

3 Answers3

5

This is the regex I use for general IP validation:

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

Breakdown:

1.`[0-9](?!\d)`       -> Any Number 0 through 9 (The `(?!\d)` makes sure it only grabs stand alone digits)
2.`|[1-9][0-9](?!\d)` -> Or any number 10-99 (The `(?!\d)` makes sure it only grabs double digit entries)
3.`|1[0-9]{2}`        -> Or any number 100-199
4.`|2[0-4][0-9]`      -> Or any number 200-249
5.`|25[0-5]`          -> Or any number 250-255
6.`[.]?`              -> With or without a `.`
7.`{4}`               -> Lines 1-6 exactly 4 times

This hasn't failed my yet for IP address validation.

For your specific case, this should do it:

(169\.254\.)((([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-4])[.])(([0-9](?!\d)|[1-9][0-9](?!\d)|1[0-9]{2}|2[0-4][0-9]|25[0-5])))

This is very long because I couldn't figure out how to get 169.254.(0-254).255 to check without getting 169.254.255.1 to fail

Edit: Fixed due to comments

Nick
  • 4,026
  • 2
  • 21
  • 36
  • 1
    Does not enforce the dots between octets - i.e. it erroneously matches: `1111111111`, `100.1111111`, etc. – ridgerunner Nov 01 '12 at 16:32
  • Yes, I adjusted the 2nd one for his case to force the `.` between octets, but for the primary one I don't want to force it. For the main one, it's a trade off, because I would have to nearly double the size of the regex to force the `.` between octets. I found it easier to verify `###.###.###.###` format before checking the validity of the IP address itself than to check for the IP address and the `.` at the same time. In my environment, I have much more control over the input, and I'm mainly check to make sure a `523` didn't get entered into a spread sheet instead of a `253`. – Nick Nov 01 '12 at 16:43
  • Actually, I though about it, and realized there was no reason I couldn't do the negative lookahead after each set. Now it should correctly force the dot between octets. I checked it quickly, and it correctly rejects the two examples you gave. – Nick Nov 01 '12 at 16:47
2

the regex ([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4]) matches 0-254.

see this page for more discussion

SeanC
  • 15,067
  • 5
  • 42
  • 63
  • its matching 169.254.25 for 169.254.255.1. any thoughts? – user1789564 Oct 31 '12 at 19:37
  • I would repeat the test twice, with the final digit on the 2nd section changed to 5 for the 255 match: `169\.254\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) ` – SeanC Oct 31 '12 at 20:04
1

I've written an article that provides regular expressions for all the components of a generic URI (as defined in RFC3986: Uniform Resource Identifier (URI): Generic Syntax)

See: Regular Expression URI Validation

One of the components of a generic URI is an IPv4 address. Here is the free-spacing mode Python version from that article:

re_python_rfc3986_IPv4address = re.compile(r""" ^
    # RFC-3986 URI component:  IPv4address
    (?: (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \.){3}  # (dec-octet "."){3}
        (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)         # dec-octet "."
    $ """, re.VERBOSE)

And the un-commented JavaScript version:

var re_js_rfc3986_IPv4address = /^(?:(?: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]?)$/;
ridgerunner
  • 30,685
  • 4
  • 51
  • 68