~/devtools / regex / ip-address
tool::regex-guide

regex ip-address

/^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/

IPv4 Address Validation

ad · 728×90
Regex Pattern
/^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/
Flags:none
Pattern Breakdown
^Start of string
25[0-5]250–255
2[0-4]\d200–249
1\d{2}100–199
[1-9]\d10–99
\d0–9
\.Literal dot separator
{3}Three octet+dot groups
$End of string
Matching Examples
192.168.1.1
0.0.0.0
255.255.255.0
10.0.0.1
Non-Matching Examples
256.0.0.1
192.168.1
192.168.1.1.1
abc.def.ghi.jkl

Accurately validates IPv4 addresses by ensuring each octet falls within 0–255. Unlike a simple \d{1,3} pattern, this correctly rejects values like 256 or 999.

Language Usage

JavaScriptUse RegExp object or literal syntax
const pattern = /^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/;

// Test a string
console.log(pattern.test("192.168.1.1")); // true
console.log(pattern.test("256.0.0.1")); // false

// Match and extract
const result = "192.168.1.1".match(pattern);
PythonUse the re module
import re

pattern = r"^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$"

# Test a string
if re.fullmatch(pattern, "192.168.1.1"):
    print("Valid")

# Find all matches in text
matches = re.findall(pattern, text)
JavaUse java.util.regex package
import java.util.regex.*;

String pattern = "^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)$";
Pattern p = Pattern.compile(pattern);

Matcher m = p.matcher("192.168.1.1");
boolean isValid = m.matches(); // true

// Find occurrences
Matcher finder = p.matcher(inputText);
while (finder.find()) {
    System.out.println(finder.group());
}

Common Use Cases

  • Validate IP addresses in server config files
  • Check firewall rule input
  • Extract IP addresses from log files
  • Validate input parameters for network scanners
  • Verify user-provided IPs against an allowlist

Related Patterns

email/^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-/iphone/^\+?[\d\s\-\(\)]{7,15}$/url/^https?:\/\/[^\s\/$.?#].[^\s]*$/idate/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[/
ad · 300×250
Back to Regex Tester
// related tools
Cron Expression Generator
Build and parse cron expressions visually. Generate human-readable descriptions and preview next execution times.
jwt
JWT Decoder
Decode and inspect JWT tokens. View header, payload, and signature details.
Color Converter
Convert colors between HEX, RGB, HSL, and more. Pick colors visually.
ts
Timestamp Converter
Convert Unix timestamps to human-readable dates. Supports ms/s, UTC/local, and relative time.