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 CIDR block
Regex pattern · validation

Regex for IPv4 CIDR block

Regex for CIDR notation: 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 CIDR block regex. It matches "10.0.0.0/8" and "192.168.1.0/24", and rejects "10.0.0.0/33" and "10.0.0.0". It checks the format only.

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)\/(?:3[0-2]|[12]?\d)$/
  • 10.0.0.0/8matches
  • 192.168.1.0/24matches
  • 0.0.0.0/0matches
  • 203.0.113.7/32matches
  • 10.0.0.0/33no match
  • 10.0.0.0no match
  • 10.0.0/8no match
  • 300.0.0.0/8no match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
(IPv4 address)four octets 0–255, as in the IPv4 pattern
\/a slash
(?:3[0-2]|[12]?\d)$prefix length 0–32

Examples

InputResult
10.0.0.0/8✓ Matches
192.168.1.0/24✓ Matches
0.0.0.0/0✓ Matches
203.0.113.7/32✓ Matches
10.0.0.0/33✗ No match
10.0.0.0✗ No match
10.0.0/8✗ No match
300.0.0.0/8✗ No match

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

Limitations and edge cases

It checks the format only. It does not check that the address is the network address for the prefix (10.0.0.5/8 passes).

When to use this pattern

Cloud security groups, VPN and VPC configuration, and IP allow-list inputs.

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)\/(?:3[0-2]|[12]?\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)\/(?:3[0-2]|[12]?\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)\\/(?:3[0-2]|[12]?\\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)\\/(?:3[0-2]|[12]?\\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)\/(?:3[0-2]|[12]?\d)$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for IPv4 CIDR block?
See the pattern at the top of this page. It checks the format only. It does not check that the address is the network address for the prefix (10.0.0.5/8 passes).
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 "abc10.0.0.0/8xyz" 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.