IPv4 Address Regex

^(?:(?: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]?)$

Validate IPv4 addresses with proper range checking (0-255 for each octet).

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern ^(?:(?: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]?)$:

  • ^ - Start of string
  • $ - End of string
  • ? - Optional (zero or one)
  • [...] - Character class
  • (...) - Capturing group
  • (?:...) - Non-capturing group
  • {n,m} - Quantifier (n to m times)
  • | - Alternation (OR)

Examples

Input Result Explanation
192.168.1.1 ✓ Match Private IP address
255.255.255.255 ✓ Match Broadcast address
0.0.0.0 ✓ Match Default route
256.1.1.1 ✗ No Match Out of range octet
192.168.1 ✗ No Match Missing octet

Code Examples

JavaScript

JavaScript
const pattern = /^(?:(?: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 text = "192.168.1.1";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

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]?)$'
text = "192.168.1.1"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/^(?:(?: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]?)$/';
$text = "192.168.1.1";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "^(?:(?: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]?)$";
String text = "192.168.1.1";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Network configuration, firewall rules, server logs

Frequently Asked Questions

What is the regex pattern for ipv4 address regex?
The regex 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]?)$. Validate IPv4 addresses with proper range checking (0-255 for each octet).
How do I use the ipv4 address regex pattern in JavaScript?
In JavaScript: const regex = 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]?)$'); or const regex = /^(?:(?: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]?)$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(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]?)$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for ipv4 address regex?
Network configuration, firewall rules, server logs

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: