Regex for Matching an IPv4 Address

Matches four dot-separated groups of digits in the shape of an IPv4 address.

Published October 10, 2026

This regex matches IPv4 addresses and checks that each of the four octets is between 0 and 255.

How the pattern works

Breaking the pattern down: 25[0-5] = 250–255; 2[0-4]\d = 200–249; 1\d\d = 100–199; [1-9]?\d = 0–99; {3} = three octets plus dot.

Test your regex against both matching and non-matching inputs; use the Regex tool on this site to experiment live.

Pattern tests

/^((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)$/
✓ '192.168.1.1'
✓ '0.0.0.0'
✓ '255.255.255.255'
✗ '256.1.1.1'
✗ '1.2.3'

Practical examples

The pattern

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

Test it

const re = /^((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)$/
re.test('192.168.1.1')   // true
re.test('0.0.0.0')   // true
re.test('256.1.1.1')   // false
re.test('1.2.3')   // false

In Python

import re
re.search(r'^((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)$', text)

Common mistakes

  • Using \d{1,3} and accepting 999.
  • Forgetting anchors.
  • Handling IPv6 with the same pattern.

Key takeaways

  • Anchors (^ and $) make the whole string match instead of a part of it.
  • Regex checks format only; verify data separately when it matters.
  • Test with valid and invalid samples.

FAQ

Simpler validation?

Use a library such as net.isIPv4 in Node.

Where can I test this pattern?

Use the Regex Tester on this site, or regex101.com.

More General articles