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
| Token | Description |
|---|---|
| \b | Word boundary assertion |
| (?: | Start of non-capturing group |
| (?: | Start of non-capturing group |
| 2 | Matches the literal character '2' |
| 5 | Matches the literal character '5' |
| [0-5] | Character class — matches any one of: 0-5 |
| | | Alternation — matches the expression before OR after the pipe |
| 2 | Matches the literal character '2' |
| [0-4] | Character class — matches any one of: 0-4 |
| \d | Matches 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) |
| \d | Matches any digit (0-9) |
| \d | Matches 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 |
| 2 | Matches the literal character '2' |
| 5 | Matches the literal character '5' |
| [0-5] | Character class — matches any one of: 0-5 |
| | | Alternation — matches the expression before OR after the pipe |
| 2 | Matches the literal character '2' |
| [0-4] | Character class — matches any one of: 0-4 |
| \d | Matches 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) |
| \d | Matches any digit (0-9) |
| \d | Matches 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 |
| 3 | Matches 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) |
| \d | Matches any digit (0-9) |
| ) | End of group |
| \b | Word 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
| Input | Expected |
|---|---|
| 192.168.1.0/24 | Match |
| 10.0.0.0/8 | Match |
| 192.168.1.0/33 | No Match |
| 256.0.0.0/16 | No Match |
| 172.16.0.0/12 | Match |
Try It — Interactive Tester
Match Highlighting(3 matches)
Matches & Capture Groups
95 charsFlags: gMatches: 3Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match IPv4 Addresses
/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g
Regex to Match IPv6 Addresses
/(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}/g
Regex to Match Port Numbers
/\b(?:[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])\b/g
Regex to Match MAC Addresses
/([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}/gi