IPv4 Address Regex Pattern
An IPv4 pattern that limits each octet to 0-255 and allows an optional CIDR suffix, pre-loaded in the tester. Below it, a table of edge cases shows what passes, what fails, and one ambiguity to decide on.
What this IPv4 address pattern accepts and rejects
Each of the four octets is limited to 0-255 by alternation (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?), and an optional /0 to /32 CIDR suffix follows. The one decision to make before using it is leading zeros.
Each input below was tested on its own against the pattern above when this page was built, with no flags, so ^ and $ anchor the whole value. Paste any of them into the tester to see the same result.
| Input | Result | Why it matters |
|---|---|---|
192.168.1.1 | Matches | An ordinary address. |
0.0.0.0 | Matches | The lowest address passes. |
255.255.255.255 | Matches | The highest address passes. |
10.0.0.0/24 | Matches | A CIDR prefix from /0 to /32 is accepted. |
256.100.0.1 | No match | Rejected: an octet above 255. |
10.0.0.0/33 | No match | Rejected: a prefix above /32. |
1.2.3 | No match | Rejected: three octets, although some parsers accept shortened forms like this. |
192.168.01.1 | Matches | Accepted with a leading zero, which is ambiguous: inet_aton reads 010 as octal (8), and Node's net.isIPv4 rejects the address outright. Reject leading zeros if the value reaches a system parser. |
192.168.1.1 | No match | Rejected: the anchors leave no room for surrounding whitespace, so trim input first. |
More from RegexCalc
Guides
Frequently Asked Questions
Should an IPv4 regex accept leading zeros like 192.168.01.1?
This one does, so decide deliberately. Some parsers read a leading zero as octal, so 010 can mean 8, and Node's net.isIPv4 rejects such addresses outright. If the value will reach a system parser, reject leading zeros.
Does this pattern match CIDR notation?
Yes. An optional suffix from /0 to /32 is allowed, so 10.0.0.0/24 matches and 10.0.0.0/33 does not. It does not check that the address is the network address for that prefix: 10.0.0.5/24 matches too.