比赛:D 10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10C 10.10.140.0/24 is directly connected, Vlan240不匹配:10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks255.255.255.1I 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, Vlan10In 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 masksC 10.10.140.0/24 is directly connected, Vlan240I 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 masksHere is my current code:match = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})', text)ThanksDamon 解决方案 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/1Matches:D 10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10C 10.10.140.0/24 is directly connected, Vlan240Doesn't match:10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks255.255.255.1 这篇关于正则表达式包括和排除某些IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 22:27