Regex for IPv4 Address
Regex Pattern
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$Validates a well-formed IPv4 address (0-255 in each octet)
Quick Answer
The regex pattern for ipv4 address is `^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`. Validates a well-formed IPv4 address (0-255 in each octet). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| 192.168.1.1 | ✓ Matches |
| 255.255.255.255 | ✓ Matches |
| 10.0.0.1 | ✓ Matches |
| 127.0.0.1 | ✓ Matches |
| 256.1.1.1 | ✗ No match |
| 192.168.1 | ✗ No match |
| 192.168.1.1.1 | ✗ No match |
| abc.def.ghi.jkl | ✗ No match |
Code Examples
javascript
const regex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
const isValid = regex.test(value);python
import re
pattern = r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', $value)) {
echo "valid";
}java
String pattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
IPv6 Address
Basic IPv6 address (8 groups of 4 hex digits)
MAC Address
MAC address with colons or dashes as separators
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits
URL (HTTP/HTTPS)
Validates HTTP and HTTPS URLs
URL (Any Protocol)
Matches URLs with any protocol (http, https, ftp, ws, etc.)