Regex to Match IPv4 Addresses
Validate and match IPv4 addresses with this regex pattern. Ensures each octet is within the valid 0-255 range. Free accurate IP address regex.
Regular Expression
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g
Token Breakdown
| Token | Description |
|---|---|
| \b | Word boundary assertion |
| (?: | Start of non-capturing group |
| (?: | Start of non-capturing group |
| 2 | Matches the literal character '2' |
| 5 | Matches the literal character '5' |
| [0-5] | Character class — matches any one of: 0-5 |
| | | Alternation — matches the expression before OR after the pipe |
| 2 | Matches the literal character '2' |
| [0-4] | Character class — matches any one of: 0-4 |
| \d | Matches any digit (0-9) |
| | | Alternation — matches the expression before OR after the pipe |
| [01] | Character class — matches any one of: 01 |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| \d | Matches any digit (0-9) |
| ? | Makes the preceding element optional (zero or one times) |
| ) | End of group |
| \. | Matches a literal dot |
| ) | End of group |
| {3} | Matches exactly 3 times |
| (?: | Start of non-capturing group |
| 2 | Matches the literal character '2' |
| 5 | Matches the literal character '5' |
| [0-5] | Character class — matches any one of: 0-5 |
| | | Alternation — matches the expression before OR after the pipe |
| 2 | Matches the literal character '2' |
| [0-4] | Character class — matches any one of: 0-4 |
| \d | Matches any digit (0-9) |
| | | Alternation — matches the expression before OR after the pipe |
| [01] | Character class — matches any one of: 01 |
| ? | Makes the preceding element optional (zero or one times) |
| \d | Matches any digit (0-9) |
| \d | Matches any digit (0-9) |
| ? | Makes the preceding element optional (zero or one times) |
| ) | End of group |
| \b | Word boundary assertion |
Detailed Explanation
This regex validates IPv4 addresses by ensuring each of the four octets falls within the range 0 to 255. Here is the token-by-token breakdown:
\b — A word boundary anchor that ensures the match starts at a boundary between a word character and a non-word character. This prevents partial matches within longer numbers.
(?: — Opens a non-capturing group for the first three octets (each followed by a dot).
(?:25[0-5]|2[0-4]\d|[01]?\d\d?) — A non-capturing group with three alternatives to match numbers 0-255:
- 25[0-5] matches 250-255
- 2[0-4]\d matches 200-249
- [01]?\d\d? matches 0-199. The [01]? optionally matches a leading 0 or 1, \d matches one digit, and \d? optionally matches a second digit.
. — Matches a literal dot separating the octets.
){3} — Closes the non-capturing group and repeats it exactly three times, matching the first three octets with their trailing dots.
(?:25[0-5]|2[0-4]\d|[01]?\d\d?) — The same alternation pattern again for the fourth and final octet, but without a trailing dot.
\b — Another word boundary anchor at the end.
The g flag enables global matching to find all IPv4 addresses in the text. This pattern is highly accurate because it validates the numeric range of each octet rather than just matching digit patterns.
Example Test Strings
| Input | Expected |
|---|---|
| 192.168.1.1 | Match |
| 255.255.255.255 | Match |
| 256.1.1.1 | No Match |
| 10.0.0.1 | Match |
| 192.168.1.999 | No Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
75 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match IPv6 Addresses
/(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}/g
Regex to Match CIDR Notation
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/(?:3[0-2]|[12]?\d)\b/g
Regex to Match MAC Addresses
/([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}/gi
Regex to Match Port Numbers
/\b(?:[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\b/g