PatternRegex
Match a CIDR block
Find CIDR notation like 10.0.0.0/24 in routing tables, firewall rules and infra-as-code, with the prefix limited to the valid 0–32 range.
Last updated
Matches
- The subnet 10.0.0.0/24 covers 256 addresses
- 192.168.0.0/16 is a private range
- 0.0.0.0/0 is the default route
Doesn’t match
- 10.0.0.0/33 is not a valid prefix
- 192.168.1.1 has no prefix at all
Why it’s written this way
The address side reuses the dotted-quad shape from the IP:port pattern above. What makes this one worth writing separately is the prefix: (?:3[0-2]|[12]?\d) is a genuine alternation rather than a lazy \d{1,2}, so it accepts exactly 0 through 32 — 3[0-2] covers 30–32, and [12]?\d covers 0–9, 10–19 and 20–29.
Without that alternation, a plain digit-count pattern would happily accept /45 or /99, which are not valid prefix lengths and would silently corrupt anything downstream that trusts the match.
Edge cases to know
- →Octets in the address portion are still unchecked, exactly as in the plain IP pattern — 999.999.999.999/24 matches the shape.
- →It only covers IPv4 CIDR; IPv6 prefixes (/64, /128 on a colon-separated address) need a different pattern.
- →A leading zero like /05 is accepted by [12]?\d as '05' would need the \d branch — worth checking if your source data zero-pads.