★ Web · regex
Regex for IPv4 Address
Matches IPv4 addresses with four octets from 0 to 255. Leading zeros like 01 are rejected, because some parsers read them as octal.
- Runs in your browser
- No signup
Try the ipv4 address pattern
Read it in plain English (best-effort)
- ^start of a line
- (?:start a group (not captured)
- (?:start a group (not captured)
- 25the text '25'
- [0-5]one character from: 0–5
- |or
- 2the character '2'
- [0-4]one character from: 0–4
- \da digit (0–9)
- |or
- 1the character '1'
- \da digit (0–9)
- \da digit (0–9)
- |or
- [1-9]one character from: 1–9
- ?the previous is optional
- \da digit (0–9)
- )end of group
- .the character '.'
- )end of group
- {3}exactly 3 of the previous
- (?:start a group (not captured)
- 25the text '25'
- [0-5]one character from: 0–5
- |or
- 2the character '2'
- [0-4]one character from: 0–4
- \da digit (0–9)
- |or
- 1the character '1'
- \da digit (0–9)
- \da digit (0–9)
- |or
- [1-9]one character from: 1–9
- ?the previous is optional
- \da digit (0–9)
- )end of group
- $end of a line
One sample per line works well with the m flag on anchored patterns.
The pattern, piece by piece
/^(?:(?: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)$/| Part | What it does |
|---|---|
25[0-5] | 250–255 |
2[0-4]\d | 200–249 |
1\d\d | 100–199 |
[1-9]?\d | 0–99 without a leading zero |
(?:octet\.){3} | three octets each followed by a dot |
octet$ | the fourth octet ends the string |
What it accepts and rejects
Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.
| Should match | |
|---|---|
192.168.1.1 | ✓ |
10.0.0.0 | ✓ |
255.255.255.255 | ✓ |
0.0.0.0 | ✓ |
8.8.8.8 | ✓ |
172.16.254.1 | ✓ |
| Should not match | |
|---|---|
256.1.1.1 | ✗ |
192.168.1 | ✗ |
192.168.01.1 | ✗ |
1.2.3.4.5 | ✗ |
192.168.1.-1 | ✗ |
a.b.c.d | ✗ |
Copy it into your code
RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.
const ipv4Re = /^(?:(?: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)$/;
export function isValidIpv4(value) {
return ipv4Re.test(value.trim());
}When not to use regex for this
If you need to know whether an address is private, loopback or in a CIDR range, parse it with net.ParseIP (Go), ipaddress (Python) or a library, then compare. Range logic in regex is unreadable.
Questions engineers actually ask
Why reject leading zeros like 192.168.01.1?
Does it match CIDR notation like 10.0.0.0/8?
What is catastrophic backtracking?
More web patterns
- Email AddressPragmatic email check: catches typos without rejecting real addresses.^[A-Za-z0-9_%+-]+(?:\.[A-Za-z0-9_%+-]+)*@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*\.[A-Za-z]{2,}$
- URLhttp(s) URLs with a real domain, optional port, path, query and fragment.^https?:\/\/(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}(?::\d{1,5})?(?:[\/?#]\S*)?$
- Domain NameHostnames like example.com or sub.example.co.uk, including punycode.^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$
- URL SlugLowercase kebab-case slugs like my-first-post.^[a-z0-9]+(?:-[a-z0-9]+)*$
- IPv6 AddressFull and compressed (::) IPv6 addresses.^(?:(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,7}:|(?:[0-9A-Fa-f]{1,4}:){1,6}:[0-9A-Fa-f]{1,4}|(?:[0-9A-Fa-f]{1,4}:){1,5}(?::[0-9A-Fa-f]{1,4}){1,2}|(?:[0-9A-Fa-f]{1,4}:){1,4}(?::[0-9A-Fa-f]{1,4}){1,3}|(?:[0-9A-Fa-f]{1,4}:){1,3}(?::[0-9A-Fa-f]{1,4}){1,4}|(?:[0-9A-Fa-f]{1,4}:){1,2}(?::[0-9A-Fa-f]{1,4}){1,5}|[0-9A-Fa-f]{1,4}:(?::[0-9A-Fa-f]{1,4}){1,6}|:(?:(?::[0-9A-Fa-f]{1,4}){1,7}|:))$
- Hex ColorCSS hex colours: #rgb, #rgba, #rrggbb and #rrggbbaa.^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$
- HTML TagFinds opening, closing and self-closing tags in a string. Not a parser.<\/?([A-Za-z][A-Za-z0-9-]*)\b[^<>]*>
Or browse the full regex library.
More from the workbench
Fine print
Tools: on the house.
Engineer: by the hour.
The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.
I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.
