Regex for IPv4 Address

Accurate mathematical IPv4 validator regex enforcing strict 0-255 bounds across all 4 dot-separated octets without false positives.

/

⚡ Live Interactive Sandbox

Validation Result: MATCH ✓

Pattern Breakdown & Tokens

  • • 25[0-5] — octet numbers from 250 through 255
  • • 2[0-4][0-9] — numbers from 200 through 249
  • • [01]?[0-9][0-9]? — numbers from 0 through 199
  • • \. — escaped dot delimiter between octets
  • • {3} — three octet repetitions followed by final octet

Code Snippets

JavaScript
const ipRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const isValid = ipRegex.test(ip);
Python
import re
pattern = r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
is_valid = bool(re.match(pattern, ip))
Go (Golang)
matched, err := regexp.MatchString(`^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`, ip)
PHP
$isValid = preg_match('/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $ip);

Open the full tool: Open Full Regex Tester →

FAQ

How does this regular expression for IPv4 Address work?

Accurate mathematical IPv4 validator regex enforcing strict 0-255 bounds across all 4 dot-separated octets without false positives. The pattern is: ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$

How to use this regex in JavaScript and Python?

In JavaScript: new RegExp("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$").test(input). In Python: re.match(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$", input).

Can I test complex regular expressions online?

Yes, use our full in-browser Regex Tester tool with live syntax highlighting, group capture table, and zero server uploads.

🔗 Share this page

TelegramWhatsAppX