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

Frequently Asked Questions

What is the regex for IPv4 Address?
The regex pattern for IPv4 Address is /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/. Validates IPv4 addresses (four octets from 0-255 separated by dots).
How do I use the IPv4 Address regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the IPv4 Address pattern match?
It matches strings like 192.168.1.1; it rejects strings like 256.1.1.1. See the Examples section above for the full list of matching and non-matching cases.

Related Tools