正则表达式:如何匹配 RFC1918 私有 IPV4 地址范围中的 IP 地址(在 Python 中)? [英] Regex: How to match IP address in RFC1918 private IPV4 address ranges (in Python)?

查看:87
本文介绍了正则表达式:如何匹配 RFC1918 私有 IPV4 地址范围中的 IP 地址(在 Python 中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RFC1918 将私有 IPv4 地址定义为属于以下任一范围的地址:

RFC1918 defines private IPv4 addresses as those that fall within any of the following ranges:

10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255

出于分析目的,我将 127.0.0.1 添加到此列表中.我知道有经过试验和测试的正则表达式来匹配任何 IPv4 地址,但是我如何将其中之一缩小到仅当地址落在上述范围之一或 127.0.0.1 中时才匹配?将使用 Python.

I'm adding 127.0.0.1 to this list, for the purposes of my analysis. I know there are tried-and tested regex's to match any IPv4 address, but how would I narrow one of these to down to matching only if the address falls in one of the above ranges or in 127.0.0.1? Will be using Python.

非常感谢

推荐答案

以下正则表达式应该有效:

The following regexp should work:

^(?:10|127|172\.(?:1[6-9]|2[0-9]|3[01])|192\.168)\..*

演示

这篇关于正则表达式:如何匹配 RFC1918 私有 IPV4 地址范围中的 IP 地址(在 Python 中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆