Skip to main content
hank-builds.com

IPv4 Address regex

Matches valid IPv4 addresses (0.0.0.0 to 255.255.255.255).

/\b(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g

What it matches

Four octets separated by dots, each held to 0-255 by the alternation 25[0-5] | 2[0-4]\d | [01]?\d\d?. The word boundaries at either end stop it matching four numbers inside a longer dotted string, so a version like 1.2.3.4.5 is not read as an address.

Railroad diagram

Read left to right. Every path through the diagram is a string the pattern accepts.

\b "2" "5" [0-5] "2" [0-4] \d [01] \d \d \. "2" "5" [0-5] "2" [0-4] \d [01] \d \d \. "2" "5" [0-5] "2" [0-4] \d [01] \d \d \. "2" "5" [0-5] "2" [0-4] \d [01] \d \d \b

Drag to pan. Ctrl+scroll to zoom.

Breakdown

gFind all matches instead of stopping after the first
\bWord boundary
2Literal "2"
5Literal "5"
[0-5]Match one of: Literal "0"–Literal "5"
2Literal "2"
[0-4]Match one of: Literal "0"–Literal "4"
\dDigit (0-9)
[01]?Match one of: Literal "0", Literal "1", optional
\dDigit (0-9)
\d?Digit (0-9), optional
\.Literal "."
2Literal "2"
5Literal "5"
[0-5]Match one of: Literal "0"–Literal "5"
2Literal "2"
[0-4]Match one of: Literal "0"–Literal "4"
\dDigit (0-9)
[01]?Match one of: Literal "0", Literal "1", optional
\dDigit (0-9)
\d?Digit (0-9), optional
\.Literal "."
2Literal "2"
5Literal "5"
[0-5]Match one of: Literal "0"–Literal "5"
2Literal "2"
[0-4]Match one of: Literal "0"–Literal "4"
\dDigit (0-9)
[01]?Match one of: Literal "0", Literal "1", optional
\dDigit (0-9)
\d?Digit (0-9), optional
\.Literal "."
2Literal "2"
5Literal "5"
[0-5]Match one of: Literal "0"–Literal "5"
2Literal "2"
[0-4]Match one of: Literal "0"–Literal "4"
\dDigit (0-9)
[01]?Match one of: Literal "0", Literal "1", optional
\dDigit (0-9)
\d?Digit (0-9), optional
\bWord boundary

Test cases

Matches

  • 192.168.1.1
  • 10.0.0.255
  • 8.8.8.8

Does not match

  • 999.999.999.999
  • 256.1.1.1
  • 192.168.1

Caveats

It accepts leading zeros, so 010.1.1.1 passes here while some parsers read that octet as octal. It says nothing about whether an address is routable, private or reserved: 0.0.0.0 and 127.0.0.1 both match. There is no IPv6 support; that needs a very different pattern.

Test this against your own input in the regex tester, browse the full pattern library, or check the syntax cheat sheet.