IPv4 Address Regex
Match dotted-decimal IPv4 addresses with each of the four numbers held between 0 and 255. Leading zeros are refused, because some parsers read them as octal.
One case per line; the m flag makes ^ and the end anchor work line by line.
Link optionspattern only
The address bar holds the pattern, flags and replacement, so Copy link shares them. Your test text stays out of it unless you include it (up to 2,000 characters), because it may be private. Nothing is sent to a server.
Matches
6matches
The first is “192.168.1.1”, at position 0.
- Groups
- none
- Characters matched
- 62 of 138
What the pattern meansHover or tap a part to see it in the pattern and what it matched
How this works: Method, 5 sources, Checked against 1 worked example,
How this works
Method
Your pattern runs in your own browser’s JavaScript engine, in a background worker that is stopped after 1.5 seconds, so a pattern that backtracks catastrophically can’t freeze the page. The explanation comes from our own parser of the ECMAScript pattern grammar, checked against the engine; what each part matched is found by wrapping that part in one more group and running the pattern again. Conversions to other flavours only rewrite the syntax and list what their documentation says works differently.
Sources
How it’s tested
One worked example for this page is checked by automated tests before every release: given the inputs, the tool must show the expected answer.
Changes
- First published, with a library of 19 common patterns and their test cases.
Worked example
Take 192.168.1.1, the first case in the tester. Reading the pattern left to right, each part takes its share of the text:
^The start of the text (matches a position, no characters)(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}A group (not captured), exactly three times: matches 192.168.1.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)A group (not captured), either of four alternatives: matches 1$The end of the text (matches a position, no characters)
How the IPv4 regex keeps each octet between 0 and 255
A regex cannot compare numbers, so the range 0 to 255 is spelt out as text in four alternatives. 25[0-5] covers 250 to 255, 2[0-4]\d covers 200 to 249, 1\d\d covers 100 to 199, and [1-9]?\d covers 0 to 99. The order matters only for speed here, since the anchors force the whole octet to match.
The last alternative is where leading zeros are ruled out: an optional non-zero digit followed by one digit allows 7 and 42, but never 07 or 007. (?:…\.){3} repeats the octet and its dot three times, and a fourth octet ends the address without a trailing dot.
Because the octet is written once and reused, changing the rules (for example, to accept leading zeros with [01]?\d\d?) is a single edit.
- The URL Standard’s IPv4 number parser treats a part that starts with 0 as octal and one that starts with 0x as hexadecimal, and flags both as validation errors. Source: WHATWG URL Standard, IPv4 number parser.
- An IPv4 address is a fixed four octets (32 bits); written as dotted decimal, each octet is one number from 0 to 255. Source: RFC 791, Internet Protocol, section 2.3 (addressing).
Test cases
Every case runs as an automated test of this page’s pattern, so the table can’t drift from what the pattern really does.
| Text | Result | Why |
|---|---|---|
| 192.168.1.1 | Passes | a typical home router |
| 10.0.0.255 | Passes | a zero and a 255 octet |
| 8.8.8.8 | Passes | single-digit octets |
| 0.0.0.0 | Passes | the all-zero address |
| 255.255.255.255 | Passes | the broadcast address, the top of every range |
| 172.16.254.3 | Passes | three-digit octets below 255 |
| 256.1.1.1 | Fails | 256 is one past the largest octet |
| 192.168.1 | Fails | only three octets |
| 192.168.01.1 | Fails | a leading zero, which some parsers treat as octal |
| 1.2.3.4.5 | Fails | five octets |
| 192.168.1.1/24 | Fails | CIDR notation is a different format |
| 192.168.1.-1 | Fails | a negative number |
What it doesn’t check
- It checks the format only; it does not know about private, reserved or multicast ranges, so filter those separately if you need public addresses.
- Leading zeros are rejected on purpose. Browsers following the URL Standard read 010 as octal 8, so 010.0.0.1 is really 8.0.0.1, and accepting it can hide a different address than the one shown.
- Shorthand forms that some tools accept, such as 127.1 or a single 32-bit number, do not match.
- Add a prefix length with a separate pattern, such as a slash followed by 0 to 32, when you need CIDR blocks.
The same pattern in other languages
Converted automatically from the JavaScript version; the tester above always runs JavaScript.
| Flavour | Pattern | Notes |
|---|---|---|
| Python | (?a)^(?:(?: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)\Z | |
| PCRE | ^(?:(?: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)\z | |
| Go | ^(?:(?: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)$ | |
| Java | ^(?:(?: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)\z | |
| .NET | ^(?:(?: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)\z | In .NET, \d and \w match any Unicode digit or letter. Pass RegexOptions.ECMAScript, or write [0-9] and [A-Za-z0-9_], for JavaScript’s ASCII-only meaning. |
Sources
Frequently Asked Questions
Why not just use \d{1,3} for each part?
Three digits allow 999, so 999.999.999.999 would pass. Spelling out the ranges 250 to 255, 200 to 249, 100 to 199 and 0 to 99 is the only way a regex can stop at 255.
How do I allow leading zeros like 192.168.001.001?
Replace the last alternative of the octet with an optional zero or one followed by one or two digits. Only do this when the addresses come from a source that writes them that way, since many network tools read a leading zero as octal.
How do I match an IPv4 address inside a log line?
Remove the start and end anchors and wrap the pattern in word boundaries, then search with the g flag. Without the boundaries, a version number such as 1.2.3.4.5 would contain a match.