elefcode

Regex pattern

IPv4 Regex — free online tester

Strictly match IPv4 addresses — rejects out-of-range octets like 256.0.0.1 that simpler patterns let through.

The pattern

/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\b/g

How it works

A naive `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}` regex matches 999.999.999.999 — clearly wrong. This stricter version uses alternation per octet (0–255) so only valid IPv4 addresses match. Word boundaries on both ends prevent it from matching inside longer numbers.

When to use it

  • Parse IPs from access logs (nginx, Apache, application logs)
  • Extract suspicious IPs from a fail2ban report
  • Validate user-supplied IP addresses in configs
  • Filter ranges in firewall / CDN rule preview

Tip

This matches every IPv4 address but doesn't enforce CIDR notation or distinguish public from private (RFC 1918) ranges — add another filter for that.

Try it on this input

Server: 192.168.1.1
Gateway: 10.0.0.1
DNS: 8.8.8.8 and 8.8.4.4
Public test: 93.184.216.34 (example.com)
Loopback: 127.0.0.1
Not valid: 300.1.1.1, 256.0.0.0, 1.2.3, 999.999.999.999

What this pattern doesn't catch

  • Does not validate or extract CIDR suffixes (e.g. /24 not matched)
  • No distinction between public, private, and reserved ranges
  • Won't catch IPv6 addresses — use a separate pattern for those

More regex patterns