Back to the tool

Regex to Validate an IPv4 Address: the One Pattern Worth Keeping

The regex to validate an IPv4 address that most people copy looks like this: ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$. It is also the regex that accepts 999.999.999.999 as a valid IP address. If your pattern cannot tell 999 from 255, it is checking the shape of an address, not whether the address exists.

Here is the pattern that actually checks the range. Learn it once and you will never need another IPv4 regex:

^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

The whole trick is in the octet. An octet is 0 to 255, and you cannot say that with a single character class, so the pattern splits the range into three alternatives. 25[0-5] covers 250 to 255. 2[0-4][0-9] covers 200 to 249. [01]?[0-9][0-9]? covers 0 to 199. Each octet tries all three, and if none fits, the address fails. The outer structure is simple: three octets followed by dots, then one final octet, all pinned between ^ and $ so a longer string cannot sneak a match through.

IPv4 regex validation: the test cases that prove it

I ran the pattern above against a small truth table in Python before writing this. Every row below is what the engine actually returned, not what I hoped it would:

InputResultWhy
192.168.1.1matchordinary address
0.0.0.0matchall zeros is valid syntax
255.255.255.255matchtop of every range
10.0.0.255match255 hits the first alternative
256.100.50.25no match256 is out of range
192.168.1.300no match300 is out of range
192.168.1no matchonly three octets
192.168.1.1.1no matchfive octets
999.999.999.999no matchthe case the naive pattern gets wrong
01.02.03.04matchleading zeros accepted

One more mistake to check in whatever you pasted from the internet: the dot has to be escaped. The naive pattern sometimes circulates as ^(?:[0-9]{1,3}.){3}[0-9]{1,3}$ with a bare dot, and an unescaped dot matches any character, not a literal period. 192x168y1z1 passes that version. Write \. in the pattern itself. If you are embedding the pattern in JSON or another layer that eats backslashes, the escaping doubles, which is its own way to lose an afternoon.

Two adaptation notes before you paste it anywhere. In JavaScript, drop it into a regex literal: /.../ with no flags needed. In Python, use a raw string so the backslashes survive. And never strip the anchors for a quick search, because without ^ and $ the pattern happily matches "192.168.1.1" inside "x192.168.1.1x", which is how false positives get into log parsers.

Where this breaks, and where I would stop using it. The pattern accepts leading zeros, like 192.168.001.001, while some systems read those as octal. It cannot tell a host address from a network or broadcast address. And for validating user input rather than extracting addresses from text, a parser beats any pattern: Python's ipaddress.IPv4Address raises on invalid input, and Node's net.isIP returns 4, 6, or 0. The regex is for finding addresses in messy text. The parser is for deciding whether the address is real. Use each for what it is good at.

Frequently asked questions

What is the regex for a valid IPv4 address?

The strict pattern is ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$. Each octet alternative covers one range: 250-255, 200-249, and 0-199. Keep the anchors; without them the pattern matches substrings inside longer text.

Why does my IPv4 regex accept 999.999.999.999?

Because it checks shape, not range. The common pattern ^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ only requires one to three digits per octet, and 999 is one to three digits. To reject out-of-range octets, each octet needs the three-alternative range check shown above.

How do I validate an IP address in Python or JavaScript without regex?

Use a parser. In Python, ipaddress.IPv4Address('192.168.1.1') raises AddressValueError on invalid input. In Node.js, net.isIP('192.168.1.1') returns 4, 6, or 0. Parsers also reject things the regex accepts, like leading zeros, which some systems read as octal.

Does the strict IPv4 regex accept leading zeros like 192.168.001.001?

Yes. The [01]?[0-9][0-9]? alternative matches '001' as a valid octet. If your application treats leading zeros as octal, which some C libraries and parsers do, strip or normalize leading zeros before matching, or validate with a proper IP parser.

Related reading: URL Validation Regex: Why Most Patterns Are Wrong · Phone Number Regex: Why You Should Not Write Your Own · Catastrophic Backtracking: The Regex That Took Down Cloudflare for 27 Minutes · Regex Word Boundaries Explained: the Two Characters That Stop False Matches · Regex Greedy vs Lazy Matching: The One Question Mark That Changes Everything

Try the tool

Building a pattern with boundaries? Customize a regex template and read the plain-English explanation of what each part does.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.