Regex for IP Addresses: Validate & Extract
IP Address Regex is a Regex pattern that this pattern matches valid ipv4 addresses with proper range validation (0-255 for each octet). Formula Genius generates and validates this formula automatically from a plain-English prompt.
Match and validate IPv4 addresses in text. From quick-and-dirty patterns to strict 0-255 range validation.
The Formula
"Extract all valid IPv4 addresses from a log file"
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b
This pattern matches valid IPv4 addresses with proper range validation (0-255 for each octet). It uses word boundaries (\b) to avoid matching partial numbers in longer strings.
Step-by-Step Breakdown
- \b ensures the match starts at a word boundary
- (?:25[0-5]|2[0-4]\d|[01]?\d\d?) matches 0-255 in one octet
- 25[0-5] matches 250-255
- 2[0-4]\d matches 200-249
- [01]?\d\d? matches 0-199 (with optional leading zero)
- \. matches the literal dot separator
- {3} repeats the octet+dot pattern 3 times, then one final octet
Edge Cases & Warnings
- Simple pattern \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} matches 999.999.999.999 — not a valid IP
- Leading zeros (192.168.001.001) are technically ambiguous — some systems interpret them as octal
- This pattern doesn't match IPv6 addresses — those need a separate pattern
- Embedded in URLs, the port part (:8080) won't be matched
Examples
"Server at 192.168.1.1 responded"
192.168.1.1
"Error from 10.0.0.255"
10.0.0.255
"Not an IP: 999.999.999.999"
No match (invalid range)
Frequently Asked Questions
Do I always need the strict 0-255 validation?
For extraction from trusted logs, the simpler \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} often works. For user input validation, use the strict pattern.
How do I match IPv6?
IPv6 is much more complex: [0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7} for full format. Abbreviated formats (::) need additional patterns.
Can't find what you need?
Describe any formula in plain English and Formula Genius will generate, explain, and validate it — instantly.