# Regular filtering of Intranet addresses and segments

2022-04-23 16:40:59 GokuCode

## 1. Problem description

I want to filter out ABC Intranet address and CIDR Format Intranet IP paragraph
Mainly the following paragraphs
1.A Class address :10.0.0.0～10.255.255.255
2.B Class address :172.16.0.0 ～172.31.255.255
3.C Class address :192.168.0.0～192.168.255.255

## 2. solve the problem

Regular expressions ：

``````^(10\.\d{
1,3}\.\d{
1,3}\.((0\/([89]|1[0-9]|2\d|3[012]))|(\d{
1,3})))|(172\.(1[6789]|2\\d|3[01])\.\d{
1,3}\.\d{
1,3}(\/(1[6789]|2\d|3[012]))?)|(192\.168\.\d{
1,3}\.\d{
1,3}(\/(1[6789]|2\d|3[012]))?)\$
``````

Python give an example

``````# ABC Intranet address detection
def check_intranet_ip_segment(ip):
'''  matching  1.A Class address :10.0.0.0~10.255.255.255 2.B Class address :172.16.0.0~172.31.255.255 3.C Class address :192.168.0.0~192.168.255.255  Match successfully returns True, conversely False '''
result = re.match(r'^(10\.\d{1,3}\.\d{1,3}\.((0\/([89]|1[0-9]|2\d|3[012]))|(\d{1,3})))|(172\.(1[6789]|2\\d|3[01])\.\d{1,3}\.\d{1,3}(\/(1[6789]|2\d|3[012]))?)|(192\.168\.\d{1,3}\.\d{1,3}(\/(1[6789]|2\d|3[012]))?)\$', ip)
if result:
return True
else:
return False
``````

https://yzsam.com/2022/04/202204231640214881.html