gettoolz.app

IPv4 address regex

Four numbers from 0 to 255 separated by dots, written without leading zeros. Copy the pattern, with 4 inputs it accepts and 6 it rejects, each checked against it at build time.

Part of developer tools.

Pattern^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$
JavaScript literal/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/

What the ipv4 address regex accepts and rejects

Each result below is the output of the pattern itself, run on the input when this page was built — not a claim copied next to it. A wrong verdict stops the build.

Inputs tested against the IPv4 address regex
InputResultWhy
192.168.1.1Passesa private address
8.8.8.8Passessingle-digit octets
255.255.255.255Passesthe upper bound of every octet
0.0.0.0Passesthe lower bound
256.1.1.1Failsan octet above 255
192.168.1Failsthree octets
192.168.01.1Failsa leading zero
1.2.3.4.5Failsfive octets
192.168.1.1/24FailsCIDR notation
10.0.0.1:8080Failsa port

Limits

  • Every syntactically valid address passes, including reserved, private, loopback and broadcast ones. Whether 127.0.0.1 or 10.x is acceptable in your context is a separate check.
  • Leading zeros are refused on purpose: some parsers read 010 as octal 8, so 192.168.010.1 is ambiguous rather than merely unusual.
  • IPv6 needs its own, much longer pattern; this one refuses every IPv6 address, including IPv4-mapped ones like ::ffff:192.168.1.1.

Try it on your own input

Open this pattern in the regex tester with every example above on its own line. The link adds the g and m flags so that ^ and $ hold per line and every line is tested: the highlighted lines are the ones marked as passing here. Paste your own data in place of the examples — it is matched in your browser and never uploaded.

Other common regexes

Or open the regex tester empty and bring your own pattern.