正则表达式包括和排除某些IP [英] Regex to include and exclude certain IPs
问题描述
我有一个功能齐全的python 2.7代码,可从路由表中提取IP.它仅提取x.x.x.x/xx格式的ip.但是,我确实有一个问题,排除了路由表中的某些行.
例如,这一行:
D 10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10
在这一行中,我只关心10.50.80.0/24.由于这是唯一带有/24表示法的ip,因此我只能抓住它并让regex一次忽略/(例如10.10.10.1).但是在表中,我们有以下2个异常:
10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks
C 10.10.140.0/24 is directly connected, Vlan240
我想在第二行(10.10.140.0/24)而不是第一行(10.10.60.0/16)上捕获IP.该程序正在提取IP,并检查表中是否有可用的子网.出现了10.10.60.0/16,因为它不是在表10.10.60.0/16,而是在说此子网具有可变子网划分.
当前,我的工具正在捕获该IP,并在表中标记了整个10.10.60.0/16范围,这是不正确的.我尝试了一些正则表达式编辑,但对此并不满意.我不想意外地跳过任何子网,尤其是与第一行类似的第二行.捕获所有正确的子网非常重要.
有人可以建议最好的正则表达式编辑来完成此操作.仅将具有 x.x.x.x/xx的跳过行划分为子网,x个子网,x个掩码
这是我当前的代码:
match = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})', text)
谢谢 达蒙
如果我正确地回答了您的问题,则希望您现有的正则表达式跳过任何IP地址/子网,后跟可变子网划分" .为此,您可以使用此正则表达式:
(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b(?! is variably)
- 我在您的正则表达式末尾添加了
\b(?! is variably)
末尾的 -
\b
表示单词边界 -
(?! is variably)
的前瞻性(?!
为负,可确保IP/子网后不存在文本可变".
演示: https://regex101.com/r/jTu8cj/1 >
比赛:
D 10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10
C 10.10.140.0/24 is directly connected, Vlan240
不匹配:
10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks
255.255.255.1
I have a functional python 2.7 code that extracts IPs from the routing table. It only extracts ip in x.x.x.x/xx format. I do however has a issue excluding some lines in the route table.
For example, this line:
D 10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10
In this line all I care about is 10.50.80.0/24. Since this is the only ip with /24 notation, I can only grab that and have regex ignore onces without / (e.g, 10.10.10.1). But in the table, we have below 2 anomalies:
10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks
C 10.10.140.0/24 is directly connected, Vlan240
I would like to capture the IP on second line (10.10.140.0/24) but not first line (10.10.60.0/16). The program is extracting IPs and checking if any subnet is available in table or not. 10.10.60.0/16 is issue as it is not saying that 10.10.60.0/16 is in table but only saying that this subnet has variable subnetting.
Currently my tool is capturing this IP and marking whole 10.10.60.0/16 range as in table which is not true. I tried some regex edit but was not really happy with it. I do not accidentally want to skip any subnet accidentally especially the second line that is similar to first. It is very important to capture all correct subnets.
Can someone suggest a best regex edit to accomplish this. Only skip lines that has x.x.x.x/xx is variably subnetted, x subnets, x masks
Here is my current code:
match = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})', text)
Thanks Damon
If I got your question correctly you want your existing regex to skip any IP/subnet that is followed by 'is variably subnetted'. Do that that you can use this regex:
(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b(?! is variably)
- I've added
\b(?! is variably)
at the end of your regex \b
at the end indicates a word boundary(?! is variably)
has a negative lookahead(?!
which makes sure that the text ' is variably' isn't present after the IP/subnet.
Demo: https://regex101.com/r/jTu8cj/1
Matches:
D 10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10
C 10.10.140.0/24 is directly connected, Vlan240
Doesn't match:
10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks
255.255.255.1
这篇关于正则表达式包括和排除某些IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!