Skip to content

Instantly share code, notes, and snippets.

@lt
lt / gist:4162568
Created November 28, 2012 17:07 — forked from nikic/gist:4162505
Regex to validate (IPv4) CIDR notation (with capture)
Repeating patterns, matches 0-255 in each octet, followed by /0-32
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/(3[0-2]|[1-2]?[0-9])\b
Using subpatterns to achieve the same thing.
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?1)\.(?1)\.(?1)/(3[0-2]|[1-2]?[0-9])\b