当前位置:网站首页>正则过滤内网地址和网段
正则过滤内网地址和网段
2022-04-23 16:40:00 【GokuCode】
1.问题描述
我要过滤出ABC类内网地址和CIDR格式的内网IP段
主要是以下段
1.A类地址:10.0.0.0~10.255.255.255
2.B类地址:172.16.0.0 ~172.31.255.255
3.C类地址:192.168.0.0~192.168.255.255
2.解决问题
正则表达式:
^(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举例
# ABC类内网地址检测
def check_intranet_ip_segment(ip):
''' 匹配 1.A类地址:10.0.0.0~10.255.255.255 2.B类地址:172.16.0.0~172.31.255.255 3.C类地址:192.168.0.0~192.168.255.255 匹配成功返回True,反之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
版权声明
本文为[GokuCode]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44331765/article/details/124324451
边栏推荐
- Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
- Project framework of robot framework
- Installation and management procedures
- What does cloud disaster tolerance mean? What is the difference between cloud disaster tolerance and traditional disaster tolerance?
- Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
- Construction of promtail + Loki + grafana log monitoring system
- Redis "8" implements distributed current limiting and delay queues
- 05 Lua 控制结构
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
- Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
猜你喜欢

Project framework of robot framework

Use if else to judge in sail software - use the title condition to judge

LVM与磁盘配额

NVIDIA graphics card driver error

299. Number guessing game

Construction of promtail + Loki + grafana log monitoring system

DDT + Excel for interface test

Six scenarios of cloud migration

安装及管理程序

How to upgrade openstack across versions
随机推荐
MySQL master-slave synchronization pit avoidance version tutorial
各大框架都在使用的Unsafe类,到底有多神奇?
How does flash cache data in memory?
There is a problem with the light switch from 1 to 100
阿里研发三面,面试官一套组合拳让我当场懵逼
G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
Gartner 发布新兴技术研究:深入洞悉元宇宙
面试百分百问到的进程,你究竟了解多少
JSP learning 3
最详细的背包问题!!!
File upload and download of robot framework
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
The most detailed knapsack problem!!!
Oracle data pump usage
ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
Custom implementation of Baidu image recognition (instead of aipocr)
LVM and disk quota
05 Lua 控制结构
On the security of key passing and digital signature
计组 | 【七 输入/输出系统】知识点与例题