Interesting piece: Don’t Use Regular Expressions To Parse IP Addresses! [WayBack]
TL;DR:
- for IPv4, use inet_aton [WayBack] when available
- for IPv6, use getaddrinfo [WayBack] when available
- networking – Are IP addresses with and without leading zeroes the same? – Super User It depends! [WayBack]
When have neither then for quad-dotted decimal IPv4 addresses (ignoring for instance octals and grouped quads), this is suitable: regex – Regular expression to match DNS hostname or IP Address? – Stack Overflow [WayBack]
ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
Which explained looks like this:
https://regex101.com/r/Wyr2Zd/1
Regular expression:
/
^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$
/ gExplanation:
^
asserts position at start of the string
- 1st Capturing Group
(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}
{3}
Quantifier — Matches exactly 3 times
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you’re not interested in the data
- 2nd Capturing Group
([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
- 1st Alternative
[0-9]
- Match a single character present in the list below
[0-9]
0-9
a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 2nd Alternative
[1-9][0-9]
- Match a single character present in the list below
[1-9]
1-9
a single character in the range between 1 (ASCII 49) and 9 (ASCII 57) (case sensitive)- Match a single character present in the list below
[0-9]
0-9
a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 3rd Alternative
1[0-9]{2}
1
matches the character 1 literally (case sensitive)- Match a single character present in the list below
[0-9]{2}
{2}
Quantifier — Matches exactly 2 times
0-9
a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 4th Alternative
2[0-4][0-9]
- 2 matches the character 2 literally (case sensitive)
- Match a single character present in the list below
[0-4]
0-4
a single character in the range between 0 (ASCII 48) and 4 (ASCII 52) (case sensitive)- Match a single character present in the list below
[0-9]
0-9
a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 5th Alternative
25[0-5]
25
matches the characters 25 literally (case sensitive)- Match a single character present in the list below
[0-5]
0-5
a single character in the range between 0 (ASCII 48) and 5 (ASCII 53) (case sensitive)\.
matches the character . literally (case sensitive)- 3rd Capturing Group
([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])
- 1st Alternative
[0-9]
- Match a single character present in the list below
[0-9]
0-9 a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 2nd Alternative
[1-9][0-9]
- Match a single character present in the list below
[1-9]
1-9
a single character in the range between 1 (ASCII 49) and 9 (ASCII 57) (case sensitive)- Match a single character present in the list below
[0-9]
0-9
a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 3rd Alternative
1[0-9]{2}
1
matches the character 1 literally (case sensitive)- Match a single character present in the list below
[0-9]{2}
{2}
Quantifier — Matches exactly 2 times
0-9
a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 4th Alternative
2[0-4][0-9]
2
matches the character 2 literally (case sensitive)- Match a single character present in the list below
[0-4]
0-4
a single character in the range between 0 (ASCII 48) and 4 (ASCII 52) (case sensitive)- Match a single character present in the list below
[0-9]
0-9
a single character in the range between 0 (ASCII 48) and 9 (ASCII 57) (case sensitive)- 5th Alternative
25[0-5]
25
matches the characters 25 literally (case sensitive)- Match a single character present in the list below
[0-5]
0-5
a single character in the range between 0 (ASCII 48) and 5 (ASCII 53) (case sensitive)$
asserts position at the end of the string, or before the line terminator right at the end of the string (if any)- Global pattern flags
g
modifier: global. All matches (don’t return after first match)
–jeroen