Regex for an IPv4 Address

A tested regex for IPv4 addresses that limits each octet to 0–255. Try it on valid and invalid IPs such as 192.168.1.1 and 256.1.1.1.

Part of the free Regex Tester. Runs in your browser — nothing is uploaded.

Regex for an IPv4 Address

A naive \d{1,3} would accept 999.999.999.999. The pattern here constrains every octet to the 0–255 range using alternation, so only genuine IPv4 addresses match.

Worked example: Valid and invalid IPv4 addresses

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

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

192.168.1.1
10.0.0.255
255.255.255.255
256.1.1.1
1.2.3

Result for each line

✓ match     192.168.1.1
✓ match     10.0.0.255
✓ match     255.255.255.255
✗ no match  256.1.1.1
✗ no match  1.2.3

Practical tip

The alternation 25[0-5]|2[0-4]\d|1?\d?\d covers 250–255, 200–249 and 0–199. It permits leading zeros like 01, which some parsers treat as octal.

How to use this Regex Tester

  1. 1Use the live tool above — it runs entirely in your browser and never uploads your data.
  2. 2Paste the sample from the worked example, or use your own input.
  3. 3Copy the result, then adapt the same steps in your editor, script, or pipeline.

Regex for an IPv4 Address FAQs

Does this regex accept IPv6?

No. IPv6 addresses use hexadecimal groups and colons and need a separate, much longer pattern or a parsing library.

Should I validate IPs with a regex in production?

Prefer a language library, for example Python's ipaddress module or Node's net.isIP. They also handle IPv6 and edge cases.

More Regex Tester pages

Open the full Regex Tester tool →