Skip to content
Regex for IPv4 Address — Pattern Explained with Examples

Regex for IPv4 Address — Pattern Explained with Examples

DodaTech Updated Jun 20, 2026 2 min read

IPv4 address validation is fundamental for network configuration tools, security scanners, and any application that handles IP-based communication. Each IPv4 address consists of four octets ranging from 0 to 255, separated by dots. A simple pattern checks the format, while a complete pattern enforces the 0-255 range.

The Pattern

/^(\d{1,3}\.){3}\d{1,3}$/

For strict 0-255 octet validation:

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

Pattern Breakdown

PartMeaning
^Start-of-string anchor
(\d{1,3}\.){3}Three groups of 1-3 digits followed by a dot
\d{1,3}Final octet — 1 to 3 digits
$End-of-string anchor
25[0-5]Numbers 250-255
2[0-4]\dNumbers 200-249
1\d{2}Numbers 100-199
[1-9]?\dNumbers 0-99

Matches

  • 192.168.1.1
  • 10.0.0.1
  • 255.255.255.255
  • 0.0.0.0
  • 8.8.8.8

Does NOT Match

  • 256.1.2.3 (octet out of range)
  • 192.168.1 (only 3 octets)
  • 192.168.1.1.1 (5 octets)
  • 192.168.1.abc (non-numeric)
  • .0.0.1 (leading dot)
  • 999.999.999.999 (all octets out of range)

Language Examples

JavaScript

const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/;
const strictIpv4Regex = /^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/;
console.log(strictIpv4Regex.test('192.168.1.1')); // true
console.log(strictIpv4Regex.test('256.1.2.3'));   // false

Python

import re
pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
print(bool(re.match(pattern, '192.168.1.1')))  # True
print(bool(re.match(pattern, '256.1.2.3')))    # True (simple pattern allows it)

strict = r'^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$'
print(bool(re.match(strict, '256.1.2.3')))     # False

Rust

use regex::Regex;

fn main() {
    let re = Regex::new(r"^(\d{1,3}\.){3}\d{1,3}$").unwrap();
    println!("{}", re.is_match("192.168.1.1")); // true
}

Common Pitfalls

  • The simple \d{1,3} pattern accepts numbers 000-999, so 999.999.999.999 passes format checks. Always use the full 0-255 pattern for strict validation
  • Leading zeros are valid (e.g. 192.168.001.001 is a legitimate IPv4 address), but some applications reject them
  • Partial matches within larger text (e.g. matching 192.168.1.1 inside http://192.168.1.1:8080/path) require lookarounds or separate tokenization
  • CIDR notation (e.g. 192.168.1.0/24) is a separate concept — the mask after the slash needs its own validation

Real-World Use Cases

  • Network configuration forms — validate static IP entries in router or server admin panels
  • Log analysis tools — extract and validate IP addresses from access logs for geo-location or threat detection
  • Access control lists — verify IP address entries in firewall rules or allowlist configurations

FAQ

Should I use the simple or strict IPv4 pattern?
Use the simple pattern for filtering or extraction where you just need the dotted-quad format. Use the strict 0-255 pattern for form validation where data integrity matters.
How do I handle IPv4 with CIDR notation?
Add (/(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d))?$ at the end to optionally match a slash and subnet mask. Most CIDR validators enforce 0-32 for IPv4.

Related Patterns

Regex for IPv6 Address Regex for MAC Address

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro