我有蜂箱表:
department ip
A 10.192.168.2
B 172.16.0.1
A 10.192.168.23
B 172.16.0.24
10.192.168.56
我想在ip列上使用正则表达式,这样我只能获取ip范围内的记录,如下所示
输出:
department ip
A 10.192.168.2
A 10.192.168.23
10.192.168.56
其中部门A的ip范围为10.0.0.0到10.255.255.255。
发布于 2019-06-08 23:04:49
首先,看一下这个:https://www.regular-expressions.info/ip.html
正则表达式不是用于此任务的工具,因为对于严格检查,您需要不可读的复杂正则表达式。另请阅读:Validating IPv4 addresses with regexp。严格的正则表达式看起来太复杂了。如果可能,应用简单的regexp,如下所示:
where IP rlike '^10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$'
或者,如果您需要严格检查,则按.
拆分IP地址,并检查每个八位字节的范围,如下所示,看起来比严格正则表达式(未测试)更简单:
select department, ip
from
(
select department, ip, split(ip, '\\.') i
from your_table t
)s where i[0]=10
and (i[1] between 0 and 255 )
and (i[2] between 0 and 255 )
and (i[3] between 0 and 255 );
在python中,您可以应用更优雅的解决方案并检查网络掩码/前缀,如下所示:checking-if-ipv4-address-in-network-python
https://stackoverflow.com/questions/56505282
复制相似问题