tools / regex library / ipv4 address
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

InputResult
192.168.1.1matches
10.0.0.255matches
8.8.8.8matches
127.0.0.1matches
999.0.0.1rejects
192.168.1rejects
256.256.256.256rejects

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

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