T
ToolPrime

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