IP Regex Generator

Generate a regex for IPv4, IPv6, or both.

About This Tool

The IP Regex Generator creates production-ready regular expression patterns for matching and validating IP addresses in text, logs, and configuration files. This tool generates precise regex patterns for IPv4, IPv6, or both, with optional support for port numbers and CIDR notation. Whether you're parsing log files, validating user input, or extracting IP addresses from text, these regex patterns provide accurate matching while avoiding common pitfalls like matching invalid addresses (e.g., 999.999.999.999). The generated patterns are optimized for readability and can be used directly in JavaScript, Python, Go, and most regex engines.

How to Use

  1. Select IP type: IPv4, IPv6, or Both
  2. Check "Allow :port" to match addresses with port numbers (e.g., 192.168.1.1:8080)
  3. Check "Allow /CIDR" to match addresses with CIDR notation (e.g., 10.0.0.0/24)
  4. Click "Generate" to create the regex pattern
  5. Copy the pattern and flags for use in your code
  6. Use the pattern with appropriate delimiters for your language
  7. Test the regex thoroughly with your specific use cases

Features

  • IPv4, IPv6, or combined regex generation
  • Optional port number matching (:0-65535)
  • Optional CIDR notation support (/0-32 for IPv4, /0-128 for IPv6)
  • Validates octet/hextet ranges correctly
  • Handles IPv6 compression (::) properly
  • Supports IPv6 zone identifiers (%eth0)
  • Compact and readable patterns
  • Compatible with most regex engines
  • Clear flag specifications
  • Copy-paste ready output

Common Use Cases

  • Validating IP address input in web forms
  • Parsing IP addresses from log files
  • Extracting IPs from configuration files
  • Firewall rule parsing and validation
  • Network monitoring log analysis
  • API input validation
  • Security log processing
  • IP address anonymization scripts
  • Configuration file linters
  • DNS zone file validation

Technical Details

The IP Regex Generator creates precise regular expression patterns that match valid IP addresses while rejecting invalid ones, using appropriate anchors and range constraints.

IPv4 Pattern Components:

  • Octet matching: 0-255 range validation using alternation
  • Pattern: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
  • Breakdown:
    • 25[0-5] → 250-255
    • 2[0-4][0-9] → 200-249
    • [01]?[0-9][0-9]? → 0-199
  • Four octets separated by dots: octet\.octet\.octet\.octet

IPv6 Pattern Components:

  • Hextet matching: [0-9a-fA-F]{1,4}
  • Compression handling: Matches :: zero compression
  • Multiple formats:
    • Full form: 8 hextets separated by colons
    • Compressed: :: for consecutive zeros
    • IPv4-mapped: ::ffff:192.0.2.1
  • Zone ID: Optional %interface at end

Port Matching:

  • Range: 0-65535
  • IPv4: 192.168.1.1:8080
  • IPv6: [2001:db8::1]:8080 (brackets required)
  • Pattern: :([0-9]{1,5}) with range validation
  • Rejects invalid ports like 70000

CIDR Matching:

  • IPv4: /0 to /32 (pattern: /([0-9]|[12][0-9]|3[0-2]))
  • IPv6: /0 to /128 (pattern: /([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8]))
  • Example: 192.168.1.0/24, 2001:db8::/32

Example Generated Patterns:

  • IPv4 basic: ^((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]?)$
  • IPv4 with port: Adds (:([0-9]{1,5}))? at end
  • IPv4 with CIDR: Adds (/([0-9]|[12][0-9]|3[0-2]))? at end
  • Both (IPv4 | IPv6): Alternation of both patterns

Common Pitfalls Avoided:

  • Invalid octets: Naive \d{1,3} would match 999
  • Leading zeros: Pattern accepts but doesn't require them
  • IPv6 compression: Correctly handles multiple :: scenarios
  • Port validation: Rejects ports > 65535
  • CIDR validation: Rejects /33 for IPv4, /129 for IPv6

Usage in Different Languages:

  • JavaScript: const re = /pattern/flags; re.test(str)
  • Python: import re; re.match(r'pattern', str)
  • Go: regexp.MustCompile(`pattern`)
  • Java: Pattern.compile("pattern")
  • PHP: preg_match('/pattern/', $str)

Regex Flags:

  • Typically no flags needed for IP matching
  • Case-insensitive (i) may be used for IPv6 hex digits
  • Multiline (m) if matching IPs in multi-line text
  • Global (g) for finding all IPs in a string

Testing the Regex:

Always test generated patterns with:

  • Valid addresses: 192.168.1.1, ::1, fe80::1%eth0
  • Invalid addresses: 256.1.1.1, 999.999.999.999, gggg::1
  • Edge cases: 0.0.0.0, 255.255.255.255, ::
  • With options: 192.168.1.1:80, 10.0.0.0/8

Performance Considerations:

  • IPv4 patterns are fast (simple alternation)
  • IPv6 patterns are more complex (compression handling)
  • Avoid backtracking with proper anchoring
  • Use non-capturing groups (?:) for better performance

When NOT to Use Regex:

  • When a proper IP parsing library is available (use inet_pton, ipaddress module, etc.)
  • For strict validation (regex can have false positives)
  • For complex processing (parsing is better than regex)
  • Regex is best for: log parsing, quick validation, text extraction

Example Use Cases:

  • Log parsing: Extract IPs from Apache/Nginx logs
  • Form validation: Validate user-entered IP addresses
  • Config parsing: Extract IPs from firewall rules
  • Security: Find IPs in incident reports or forensic data