RegexCalc Regex Engine

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.

Regular Expression JAVASCRIPT REGEX
/ / gm
Match Status
2 matches found
Pattern Valid
Recipes:
Test String
Extracted Matches

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.

InputResultWhy it matters
192.168.1.1MatchesAn ordinary address.
0.0.0.0MatchesThe lowest address passes.
255.255.255.255MatchesThe highest address passes.
10.0.0.0/24MatchesA CIDR prefix from /0 to /32 is accepted.
256.100.0.1No matchRejected: an octet above 255.
10.0.0.0/33No matchRejected: a prefix above /32.
1.2.3No matchRejected: three octets, although some parsers accept shortened forms like this.
192.168.01.1MatchesAccepted 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.1No matchRejected: the anchors leave no room for surrounding whitespace, so trim input first.

More from RegexCalc

Guides

All tools →All 13 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.

Developer Sponsor / Partner
Copied to clipboard!