Regex to Match CIDR Notation

Validate CIDR notation (IPv4 with subnet prefix length) using this regex. Matches network addresses like 192.168.1.0/24 with proper ranges.

Regular Expression

/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\/(?:3[0-2]|[12]?\d)\b/g

Token Breakdown

TokenDescription
\bWord boundary assertion
(?:Start of non-capturing group
(?:Start of non-capturing group
2Matches the literal character '2'
5Matches the literal character '5'
[0-5]Character class — matches any one of: 0-5
|Alternation — matches the expression before OR after the pipe
2Matches the literal character '2'
[0-4]Character class — matches any one of: 0-4
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
[01]Character class — matches any one of: 01
?Makes the preceding element optional (zero or one times)
\dMatches any digit (0-9)
\dMatches any digit (0-9)
?Makes the preceding element optional (zero or one times)
)End of group
\.Matches a literal dot
)End of group
{3}Matches exactly 3 times
(?:Start of non-capturing group
2Matches the literal character '2'
5Matches the literal character '5'
[0-5]Character class — matches any one of: 0-5
|Alternation — matches the expression before OR after the pipe
2Matches the literal character '2'
[0-4]Character class — matches any one of: 0-4
\dMatches any digit (0-9)
|Alternation — matches the expression before OR after the pipe
[01]Character class — matches any one of: 01
?Makes the preceding element optional (zero or one times)
\dMatches any digit (0-9)
\dMatches any digit (0-9)
?Makes the preceding element optional (zero or one times)
)End of group
\/Matches a literal forward slash
(?:Start of non-capturing group
3Matches the literal character '3'
[0-2]Character class — matches any one of: 0-2
|Alternation — matches the expression before OR after the pipe
[12]Character class — matches any one of: 12
?Makes the preceding element optional (zero or one times)
\dMatches any digit (0-9)
)End of group
\bWord boundary assertion

Detailed Explanation

This regex validates CIDR (Classless Inter-Domain Routing) notation, which combines an IPv4 address with a subnet prefix length. Here is the token-by-token breakdown:

\b — A word boundary anchor ensuring the match does not start in the middle of another token.

(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?).){3} — Matches the first three octets of the IPv4 address, each followed by a dot. The inner alternation ensures each octet is in the valid range of 0-255: 25[0-5] covers 250-255, 2[0-4]\d covers 200-249, and [01]?\d\d? covers 0-199.

(?:25[0-5]|2[0-4]\d|[01]?\d\d?) — Matches the fourth octet using the same range-checking alternation.

/ — Matches a literal forward slash separating the IP address from the prefix length.

(?:3[0-2]|[12]?\d) — Matches the prefix length, which must be a number from 0 to 32. The alternation works as follows: 3[0-2] matches 30-32, and [12]?\d matches 0-29. The optional [12]? allows single-digit values (0-9), teens (10-19), and twenties (20-29).

\b — A closing word boundary anchor.

The g flag enables global matching. CIDR notation is used extensively in network configuration, firewall rules, and routing tables to specify IP address ranges and subnet masks.

Example Test Strings

InputExpected
192.168.1.0/24Match
10.0.0.0/8Match
192.168.1.0/33No Match
256.0.0.0/16No Match
172.16.0.0/12Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(3 matches)

192.168.1.0/24 10.0.0.0/8 192.168.1.0/33 256.0.0.0/16 172.16.0.0/12

Matches & Capture Groups

#1192.168.1.0/24index 0
#210.0.0.0/8index 15
#3172.16.0.0/12index 54
Pattern: 95 charsFlags: gMatches: 3

Ctrl+Shift+C to copy regex

Customize this pattern →