RegexBuilder · regex answers
Regex for an IPv4 address
/^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/The trap in IPv4 is the range: \d{1,3} happily accepts 999.999.999.999. Each octet has to be spelled out as four alternatives covering 250-255, 200-249, 100-199 and 0-99.
What each part does
| 25[0-5] | 250 through 255 |
| 2[0-4]\d | 200 through 249 |
| 1\d\d | 100 through 199 |
| [1-9]?\d | 0 through 99, with no leading zero |
| ( ... \.){3} | that octet, followed by a dot, exactly three times |
Tested against real input
Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.
| Input | Result | Matched |
|---|---|---|
| 192.168.0.1 | matches | 192.168.0.1 |
| 8.8.8.8 | matches | 8.8.8.8 |
| 255.255.255.255 | matches | 255.255.255.255 |
| 0.0.0.0 | matches | 0.0.0.0 |
| 256.1.1.1 | no match | — |
| 192.168.0 | no match | — |
| 192.168.0.1.5 | no match | — |
| 01.1.1.1 | no match | — |
The mistake to avoid
Writing ^\d{1,3}(\.\d{1,3}){3}$. It matches 999.999.999.999 and it matches 300.400.500.600 — it is a dot-counter, not an address check.
Try it on your own text
Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.
Open RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID
- Regex for a hex colour code