Python正则表达式以匹配IP范围 [英] Python regex to match IP range

查看:134
本文介绍了Python正则表达式以匹配IP范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python正则表达式来匹配IP范围.

I want to match IP range using a Python regex.

例如google bot IP地址范围如下

For Ex. the google bot IP range as follow

66.249.64.0-66.249.95.255

66.249.64.0 - 66.249.95.255

re.compile(r"66.249.\d{1,3}\.\d{1,3}$")

我不知道该怎么做?我发现是使用Java完成的.

I can not figure out how to do this? I found this one done using Java.

推荐答案

您可以使用此

re.compile(r"66\.249\.(?:6[4-9]|[78]\d|9[0-5])\.\d{1,3}$")

如果有动力,可以将 \ d {1,3} 替换为:

if you are motivated you can replace \d{1,3} by :

(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

说明:

正则表达式引擎不知道数字范围是多少.描述范围的唯一方法是交替写出所有可能性:

A regex engine doesn't know what a numeric range is. The only way to describe a range is to write all the possibilities with alternations:

6[4-9]  |  [78][0-9]  |  9[0-5] 

6          can be followed by    4 to 9   --> 64 to 69
7 or 8     can be followed by    0 to 9   --> 70 to 89
9          can be followed by    0 to 5   --> 90 to 95 

这篇关于Python正则表达式以匹配IP范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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