regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match an IPv4 Address

Copy the pattern, paste your log text below, and watch it highlight every IPv4 address. Below the tester: how each octet is bounded to 0–255.

Regex pattern

/\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/g

This matches a valid IPv4 address where each of the four octets is 0–255, e.g. 192.168.0.1. It rejects out-of-range octets like 999 but does not check that the address is routable or assigned.

/ /

What this pattern matches

This expression matches a full IPv4 address — four octets separated by dots — and rejects any octet above 255, so 256.1.1.1 will not match.

How it works, part by part

(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d) — one octet: 250–255, or 200–249, or 100–199, or 0–99.

(?:…\.){3} — that octet followed by a dot, repeated three times.

\b — word boundaries so it does not match inside a longer number.

IPv4 only

This does not match IPv6 addresses like 2001:db8::1, which use a different notation. Match those with a separate IPv6 pattern.

Use it in your code

Python
import re
text = "Valid: 192.168.0.1, 8.8.8.8, 255.255.255.0. Invalid: 256.1.1.1 (octet > 255), 10.0.0 (only 3 octets)."
pattern = r'''\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'''
for m in re.finditer(pattern, text):
    print(m.group())
JavaScript
const text = "Valid: 192.168.0.1, 8.8.8.8, 255.255.255.0. Invalid: 256.1.1.1 (octet > 255), 10.0.0 (only 3 octets).";
const re = new RegExp(String.raw`\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`, "g");
console.log(text.match(re));

FAQ

Does it reject octets over 255?

Yes. Each octet group caps at 255, so 256.1.1.1 does not match.

Will it match an IP embedded in a longer number?

No. The \b word boundaries stop it matching inside something like 9192.168.0.1.

Does it match IPv6?

No. IPv6 uses colon-separated hex groups and needs its own pattern.