regex pattern · ready to copy
Regex for matching IPv4 addresses
Match valid 0-255 octets, not just `\d{1,3}` four times.
intermediate
javascript / pcre / python4 use cases
The pattern
\b(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}\b
Test cases
| Input | Result |
|---|---|
| 192.168.1.1 | ✓matches |
| 10.0.0.255 | ✓matches |
| 8.8.8.8 | ✓matches |
| 127.0.0.1 | ✓matches |
| 999.0.0.1 | ✗rejects |
| 192.168.1 | ✗rejects |
| 256.256.256.256 | ✗rejects |
Edge cases & caveats
Doesn't enforce private vs public ranges. For RFC-1918 only: `(?:10\.|172\.(?:1[6-9]|2\d|3[01])\.|192\.168\.)`. Use `\b` boundaries to avoid matching `999.999.999.999`.
Common use cases
- log file IP extraction
- firewall rule audit
- abuse detection
- geo-IP lookup pipeline
Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab
Related
bitcoin address · hashtag · hex color · html tag · ↗ generate RFC 5737 test IPs