Regular Expressions 101

Community Patterns

IPv4 private CIDR range RFC1918

1

Regular Expression
Golang

`
^(10(\.(([0-9]?[0-9])|(1[0-9]?[0-9])|(2[0-4]?[0-9])|(25[0-5]))){3}/([8-9]|(1[0-9])|(2[0-9])|(3[0-1])))|(172\.((1[6-9])|(2[0-9])(3[0-1]))(\.(([0-9]?[0-9])|(1[0-9]?[0-9])|(2[0-4]?[0-9])|(25[0-5]))){2}/((1[2-9])|(2[0-9])|(3[0-1])))|(192\.168(\.(([0-9]?[0-9])|(1[0-9]?[0-9])|(2[0-4]?[0-9])|(25[0-5]))){2}/((1[6-9])|(2[0-9])|(3[0-1])))|(127(\.(([0-9]?[0-9])|(1[0-9]?[0-9])|(2[0-4]?[0-9])|(25[0-5]))){3}/([8-9]|(1[0-9])|(2[0-9])|(3[0-1])))$
`
gm

Description

Validates that you provide a CIDR in one of the following valid ranges.

    127.  0.0.0 – 127.255.255.255     127.0.0.0/8
     10.  0.0.0 –  10.255.255.255      10.0.0.0/8
    172. 16.0.0 – 172. 31.255.255    172.16.0.0/12
    192.168.0.0 – 192.168.255.255   192.168.0.0/16

Subnets can be smaller (e.g. 192.168.0.0/17 is valid) but not larger (e.g. 192.168.0.0/15 is invalid). Single hosts (i.e. /32s) are invalid. Host bits are generally ignored (i.e. 192.168.1.2/16 is considered valid)

Submitted by David Stockton - 4 years ago