PatternRegex
Match an IPv4 address
Pull IPv4 addresses out of logs and config files with a short, readable pattern — and know exactly when you need the stricter octet-checking version.
Last updated
Matches
- 192.168.1.42
- 8.8.8.8
- 172.16.254.3
Doesn’t match
- 192.168.1
- 192.168..1
- abc.def.ghi.jkl
Why it’s written this way
Three repetitions of "one to three digits and a dot" followed by a final octet, wrapped in word boundaries so 1.2.3.4 inside 91.2.3.45 cannot half-match.
This is the extraction form: short, readable, and right for grep-style work over logs where the data is already machine-written and 999.999.999.999 simply does not occur.
Edge cases to know
- →It accepts out-of-range octets like 256 — each position is just \d{1,3}. Validating user input needs the long form: \b(?:(?: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)\b.
- →Version strings like 1.2.3.4 match too — an IP and a four-part version are the same shape — and a five-part string like 1.2.3.4.5 still yields its first four octets as a match. Filter by context if your text mixes them.
- →IPv6 is a different pattern entirely.