Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for IPv4 address
Regex pattern · validation

Regex for IPv4 address

Regex for IPv4 address: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use the pattern below as an IPv4 address regex. It matches "192.168.1.1" and "8.8.8.8", and rejects "256.1.1.1" and "192.168.1". Leading zeros (192.168.01.1) are rejected because some systems read them as octal.

Last updated · Maintained by FreeAIToolz · How we test

/^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/
  • 192.168.1.1matches
  • 8.8.8.8matches
  • 255.255.255.255matches
  • 0.0.0.0matches
  • 256.1.1.1no match
  • 192.168.1no match
  • 192.168.01.1no match
  • 1.2.3.4.5no match
  • 192.168.1.ano match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
25[0-5]250–255
2[0-4]\d200–249
1\d\d100–199
[1-9]?\d0–99 without leading zeros
(?:...\.){3}three octets, each followed by a dot
(...)$the fourth octet

Examples

InputResult
192.168.1.1✓ Matches
8.8.8.8✓ Matches
255.255.255.255✓ Matches
0.0.0.0✓ Matches
256.1.1.1✗ No match
192.168.1✗ No match
192.168.01.1✗ No match
1.2.3.4.5✗ No match
192.168.1.a✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Leading zeros (192.168.01.1) are rejected because some systems read them as octal. To find IPs inside text instead of validating a whole string, replace ^ and $ with \b.

When to use this pattern

Firewall rules, log parsing, network configuration forms and allow-lists.

Use it in your language

JavaScript

const re = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for IPv4 address?
See the pattern at the top of this page. Leading zeros (192.168.01.1) are rejected because some systems read them as octal. To find IPs inside text instead of validating a whole string, replace ^ and $ with \b.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abc192.168.1.1xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.