Skip to content

Instantly share code, notes, and snippets.

@Spirit-act
Created September 19, 2019 05:18
Show Gist options
  • Save Spirit-act/234527c5f54c56d0297cebdd70b54748 to your computer and use it in GitHub Desktop.
Save Spirit-act/234527c5f54c56d0297cebdd70b54748 to your computer and use it in GitHub Desktop.
Regex to match all local and private ips

Regex to match all Privates and Local IPs

Need to match:

Subnet Regex
10.0.0.0/8 ^10(\.(0|1[0-9]{0,2}|5[0-5]|[1-9]{2}|[0-4][0-9])){3}$
127.0.0.0/8 ^127(\.(0|1[0-9]{0,2}|25[0-5]|[1-9]{2}|2[0-4][0-9])){3}$
172.16.0.0/12 ^172\.((?P<f>1)?(?(f)[6-9])|(?P<s>2)?(?(s)[0-9])|(?P<t>3)?(?(t)[0-1]))(\.[0-2]([0-5]){0,2}){2}$
192.168.0.0/16 ^192\.168(\.(0|1[0-9]{0,2}|25[0-5]|[1-9]{2}|2[0-4][0-9])){2}$

Chained

^(10(\.(0\|1[0-9]{0,2}\|5[0-5]\|[1-9]{2}\|[0-4][0-9])){3}\|127(\.(0\|1[0-9]{0,2}\|25[0-5]\|[1-9]{2}\|2[0-4][0-9])){3}\|172\.((?P<f>1)?(?(f)[6-9])\|(?P<s>2)?(?(s)[0-9])\|(?P<t>3)?(?(t)[0-1]))(\.[0-2]([0-5]){0,2}){2}\|192\.168(\.(0\|1[0-9]{0,2}\|25[0-5]\|[1-9]{2}\|2[0-4][0-9])){2})$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment