Regex for an IPv4 Address
A tested regex for IPv4 addresses that limits each octet to 0–255. Try it on valid and invalid IPs such as 192.168.1.1 and 256.1.1.1.
Part of the free Regex Tester. Runs in your browser — nothing is uploaded.
Regex for an IPv4 Address
A naive \d{1,3} would accept 999.999.999.999. The pattern here constrains every octet to the 0–255 range using alternation, so only genuine IPv4 addresses match.
Worked example: Valid and invalid IPv4 addresses
Pattern /^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$/ — test strings
^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$
192.168.1.1
10.0.0.255
255.255.255.255
256.1.1.1
1.2.3Result for each line
✓ match 192.168.1.1 ✓ match 10.0.0.255 ✓ match 255.255.255.255 ✗ no match 256.1.1.1 ✗ no match 1.2.3
Practical tip
The alternation 25[0-5]|2[0-4]\d|1?\d?\d covers 250–255, 200–249 and 0–199. It permits leading zeros like 01, which some parsers treat as octal.
How to use this Regex Tester
- 1Use the live tool above — it runs entirely in your browser and never uploads your data.
- 2Paste the sample from the worked example, or use your own input.
- 3Copy the result, then adapt the same steps in your editor, script, or pipeline.
Regex for an IPv4 Address FAQs
Does this regex accept IPv6?
No. IPv6 addresses use hexadecimal groups and colons and need a separate, much longer pattern or a parsing library.
Should I validate IPs with a regex in production?
Prefer a language library, for example Python's ipaddress module or Node's net.isIP. They also handle IPv6 and edge cases.
More Regex Tester pages
- Regex for Email ValidationA practical regex for email validation with test cases. Try the pattern against valid and invalid addresses and learn why perfect email regex does not exist.
- Regex for Phone NumbersTest a regex for phone numbers that accepts common US and international formats such as +1 555-123-4567. Try your own examples live.
- Regex for URL ValidationTest a regex that matches http and https URLs. Check it against valid and invalid links and learn when to use the URL constructor instead.
- Regex for a YYYY-MM-DD DateRegex to match dates in YYYY-MM-DD format, restricting months to 01–12 and days to 01–31. Test it against valid and invalid dates.
- Regex for Hex Color CodesRegex that matches CSS hex colors like #fff and #1a2b3c. Try valid and invalid values and learn how to extend it for alpha channels.
- Regex for Password StrengthRegex that requires lowercase, uppercase, a digit and at least 8 characters, using lookaheads. Test passwords live and see why length matters most.
- Regex for Username ValidationA username regex allowing letters, numbers and underscores with 3–16 characters. Test valid and invalid names and adapt the rules to your app.
- Regex to Extract Numbers From a StringExtract all numbers from text with the global regex \d+. See the matches, then extend the pattern for decimals and negative numbers.
- Regex for US ZIP CodesRegex that matches US ZIP codes in 5-digit and ZIP+4 formats. Test valid and invalid values like 90210 and 90210-1234.