Validating IPv4 addresses with regexp
Categories:
Mastering IPv4 Address Validation with Regular Expressions

Learn to construct robust regular expressions for accurately validating IPv4 addresses, covering common pitfalls and best practices.
Validating user input is a critical aspect of secure and reliable software development. Among the various data types, IPv4 addresses present a unique challenge due to their specific format and range constraints. Regular expressions (regex) offer a powerful and concise way to perform this validation. This article will guide you through crafting effective regex patterns to ensure the IPv4 addresses you process are syntactically correct and within the valid numerical range.
Understanding IPv4 Structure and Validation Rules
An IPv4 address consists of four octets (numbers from 0 to 255) separated by dots. Each octet must adhere to specific numerical boundaries. A common mistake in regex validation is to only check for the dot-separated structure without enforcing the 0-255 range for each octet. This can lead to accepting invalid addresses like 999.999.999.999
or 1.2.3.400
.
flowchart TD A[Start Validation] --> B{Input String?} B -- No --> E[Invalid: Not a string] B -- Yes --> C{Matches `X.X.X.X` pattern?} C -- No --> E[Invalid: Format Mismatch] C -- Yes --> D{Each Octet 0-255?} D -- No --> E[Invalid: Octet Out of Range] D -- Yes --> F[Valid IPv4 Address]
Basic IPv4 Validation Logic Flow
Building the Regex: Octet by Octet
To validate an IPv4 address, we need a regex pattern that can match numbers from 0 to 255. This is typically broken down into several sub-patterns for different ranges:
- 0-9:
[0-9]
- 10-99:
[1-9][0-9]
- 100-199:
1[0-9][0-9]
- 200-249:
2[0-4][0-9]
- 250-255:
25[0-5]
Combining these with the OR operator (|
) gives us a comprehensive pattern for a single valid octet. Then, we repeat this pattern four times, separated by escaped dots (\.
).
^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$
Comprehensive Regex for IPv4 Address Validation
^
and $
anchors are crucial. ^
asserts the start of the string, and $
asserts the end. Without them, the regex might match an IPv4 address embedded within a larger string (e.g., abc192.168.1.1def
).Practical Implementation Examples
Here's how you can use this regex in various programming languages. The core regex remains the same, but the implementation details for matching and testing will differ.
Python
import re
ipv4_regex = r"^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$"
def is_valid_ipv4(ip_address): return re.match(ipv4_regex, ip_address) is not None
print(f"192.168.1.1 is valid: {is_valid_ipv4('192.168.1.1')}") print(f"255.255.255.255 is valid: {is_valid_ipv4('255.255.255.255')}") print(f"0.0.0.0 is valid: {is_valid_ipv4('0.0.0.0')}") print(f"1.2.3.400 is valid: {is_valid_ipv4('1.2.3.400')}") print(f"999.999.999.999 is valid: {is_valid_ipv4('999.999.999.999')}")
JavaScript
const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])$/;
function isValidIPv4(ipAddress) { return ipv4Regex.test(ipAddress); }
console.log(192.168.1.1 is valid: ${isValidIPv4('192.168.1.1')}
);
console.log(255.255.255.255 is valid: ${isValidIPv4('255.255.255.255')}
);
console.log(0.0.0.0 is valid: ${isValidIPv4('0.0.0.0')}
);
console.log(1.2.3.400 is valid: ${isValidIPv4('1.2.3.400')}
);
console.log(999.999.999.999 is valid: ${isValidIPv4('999.999.999.999')}
);