IP Regex Generator

Generate safe and reliable regular expressions for IPv4 and IPv6 address validation

IP Address Type

Generated Pattern

Regular Expression

Test on RegexR
/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$/
IPv4 address pattern with configurable validation options

Test Cases

Should Match (5)
Should Not Match (5)

Pattern Info

Trade-offs

  • Short pattern optimized for common use cases
  • Uses exact match anchors for strict validation

Recommendations

  • For production use, combine regex validation with dedicated IP parsing libraries
  • Test regex performance with your expected input size and patterns
  • Always validate regex patterns against your specific use case requirements
  • Consider semantic validation beyond pattern matching (reserved ranges, etc.)

Implementation Examples

const ipRegex = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$/;
const isValid = ipRegex.test(ipAddress);
import re
pattern = r'^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$'
is_valid = bool(re.match(pattern, ip_address))
$pattern = '/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$/';
$isValid = preg_match($pattern, $ipAddress);
use regex::Regex;
let re = Regex::new(r"^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$").unwrap();
let is_valid = re.is_match(&ip_address);

Add regex = "1.0" to Cargo.toml dependencies

import "regexp"
re := regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$`)
isValid := re.MatchString(ipAddress)
import java.util.regex.Pattern;
Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$");
boolean isValid = pattern.matcher(ipAddress).matches();
pattern = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$/
is_valid = !!(ip_address =~ pattern)
if [[ "$ip_address" =~ ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d))$ ]]; then
  echo "Valid IP"
fi

Bash regex is case-sensitive by default

IP Address Format & RegEx Validation

Understanding IPv4 and IPv6 address formats and how to validate them properly using regular expressions.

Understanding IP Address Formats

IP addresses come in two versions: IPv4 (32-bit) and IPv6 (128-bit). Each has specific formatting rules that programmers need to understand for proper validation. The key to validation is understanding the structure, not just pattern matching. RegEx can help with basic format checking, but proper IP validation often requires additional logic.

IPv4 Address Format

IPv4 addresses consist of four decimal numbers (0-255) separated by dots. Structure: X.X.X.X where each X is 0-255 Examples: 192.168.1.1, 10.0.0.1, 127.0.0.1 Key Rules: • Each octet ranges from 0 to 255 • Leading zeros are typically not allowed (001.002.003.004) • All four octets are required Common Validation Mistakes: • Allowing values > 255 (like 256.1.1.1) • Missing boundary checks • Not handling edge cases like 0.0.0.0

IPv6 Address Format

IPv6 addresses are 128-bit addresses written as eight groups of four hexadecimal digits, separated by colons. Full Format: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 Compressed: 2001:db8:85a3::8a2e:370:7334 (:: replaces consecutive zeros) Key Rules: • Eight groups of 1-4 hex digits (0-9, a-f, A-F) • Leading zeros can be omitted in each group • Consecutive zero groups can be replaced with :: (only once per address) • Case insensitive Special Cases: • :: (all zeros address) • ::1 (loopback) • IPv4-mapped: ::ffff:192.168.1.1

RegEx Validation Approach

Regular expressions can handle basic format validation, but IP validation has nuances that make pure RegEx challenging. IPv4 RegEx Challenges: • Simple patterns like \d+\.\d+\.\d+\.\d+ don't check ranges • Proper range validation (0-255) requires complex patterns • Leading zero handling varies by use case IPv6 RegEx Challenges: • Multiple valid representations of the same address • Compression rules (:: usage) • Mixed IPv4/IPv6 notation support Best Practice: Use RegEx for initial format checking, then validate with programming logic for range checks and special cases. Most languages have built-in IP parsing functions that handle edge cases correctly.

Example Patterns

Basic IPv4 Pattern

^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$

Matches basic dot-decimal format, but doesn't validate ranges

Matches: 192.168.1.1, 10.0.0.1
Fails: 192.168.1, 192.168.1.1.1
Limitation: Allows invalid ranges like 999.999.999.999

Range-Validated IPv4

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

Validates each octet is 0-255

Matches: 192.168.1.1, 255.255.255.255, 0.0.0.0
Fails: 256.1.1.1, 192.168.1, 192.168.01.1
Limitation: Complex and hard to maintain

Basic IPv6 Pattern

^[0-9a-fA-F:]+$

Very basic check for hex digits and colons

Matches: 2001:db8::1, ::1
Fails: 192.168.1.1, hello
Limitation: Doesn't validate structure or compression rules

Practical Validation Tips

For Production Code: 1. Use language built-ins (inet_aton, IPAddress.parse, etc.) 2. RegEx for quick format checks only 3. Always validate ranges programmatically 4. Consider your use case (strict vs lenient parsing) For Learning/Testing: 1. Start with basic patterns 2. Add complexity gradually 3. Test edge cases thoroughly 4. Compare with known-good implementations Common Gotchas: • 192.168.1.01 (leading zeros) • 192.168.1 (missing octets) • 192.168.1.1.1 (extra octets) • Case sensitivity in IPv6 • Multiple :: in IPv6

Key Recommendations

Use Built-in Functions

Most languages have reliable IP parsing libraries. Use them instead of rolling your own RegEx.

Test Edge Cases

Test boundary values, malformed inputs, and different valid representations.

Consider Performance

Complex RegEx patterns can be slow. Profile your validation code with realistic data.

Validate Context

Consider what you're doing with the IP. Some contexts require stricter validation than others.