RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for an IPv4 address

/^((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)$/

The trap in IPv4 is the range: \d{1,3} happily accepts 999.999.999.999. Each octet has to be spelled out as four alternatives covering 250-255, 200-249, 100-199 and 0-99.

What each part does

25[0-5]250 through 255
2[0-4]\d200 through 249
1\d\d100 through 199
[1-9]?\d0 through 99, with no leading zero
( ... \.){3}that octet, followed by a dot, exactly three times

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
192.168.0.1matches192.168.0.1
8.8.8.8matches8.8.8.8
255.255.255.255matches255.255.255.255
0.0.0.0matches0.0.0.0
256.1.1.1no match
192.168.0no match
192.168.0.1.5no match
01.1.1.1no match

The mistake to avoid

Writing ^\d{1,3}(\.\d{1,3}){3}$. It matches 999.999.999.999 and it matches 300.400.500.600 — it is a dot-counter, not an address check.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers