Regex for IPv4 Address

All patterns
Validation67 chars

Regex for IPv4 Address

Use this when you need to validate dotted IPv4 literals and reject octets outside the 0-255 range.

The expression

/^(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$/

How to read it

Anchor: the match must start at the beginning of the string.

Anchor: the match must run to the end of the string.

\d matches a single digit, 0 through 9.

A brace quantifier fixes how many times the previous token repeats.

(?: … ) groups without capturing, so it costs nothing to read back.

Validate your own text, line by line

no input
Try:

This expression is anchored, so it asks whether a whole string is valid. Each line above is tested on its own — it will not find a match inside a longer sentence.

Examples, checked in your browser

4/4 verified

192.168.1.10

Matches.

8.8.8.8

Matches.

999.10.0.1

No match — this is the kind of input the pattern rejects.

10.0.0

No match — this is the kind of input the pattern rejects.

Use it in code

3 languages
JavaScript
const regex = /^(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$/g;
regex.test(input);
Python
import re
pattern = re.compile(r"^(?:25[0-5]|2[0-4]\d|1?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|1?\d?\d)){3}$")
bool(pattern.search(input))
Go
re := regexp.MustCompile("^(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1?\\d?\\d)){3}$")
matched := re.MatchString(input)

Other validation patterns

6
GroupValidation
Flagsnone
Examples4
SafetyOK