Regex for IPv4 Address
Validates IPv4 addresses (four octets from 0-255 separated by dots).
Pattern
/^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/ Live Tester
Enter a string to test
Examples
✓ 192.168.1.1
✓ 10.0.0.0
✓ 255.255.255.255
✗ 256.1.1.1
✗ 192.168.1
✗ 192.168.1.1.1
Pattern Breakdown
(25[0-5]|2[0-4]\d|[01]?\d\d?) — matches 0-255
\. — literal dot separator
{3} — first three octets with dots
final octet without trailing dot —
Code Snippets
Javascript
const regex = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
regex.test("192.168.1.1"); // true Python
import re
pattern = r"^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$"
bool(re.match(pattern, "192.168.1.1")) # True Php
$pattern = '/^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/';
preg_match($pattern, "192.168.1.1"); // 1 Related Patterns
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv6 Address /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/