Hello all -
I have a file which has IP address and subnet number and I use regex to extract
the IP separately from subnet.
pattern used for IP: \d{1,3}(\.\d{1,3}){3}
pattern used for subnet:((\d{1,3})|(\d{1,3}(\.\d{1,3}){1,3}))/(\d{1,2})
so I have list of ip/subnets strewn around like this
10.200.0.34
10.200.4.5
10.178.9.45
10.200/22
10.178/16
10.100.4.64/26,
10.150.100.0/28
10/8
with that above examples:
ip regex pattern works for all IP address
subnet regex pattern works for all subnets
problem now is ip pattern also matches the last 2 subnet numbers, because it
falls under ip regex.
to fix this problem, i used negative lookahead with ip pattern:
so the ip pattern now changes to:
\d{1,3}(\.\d{1,3}){3}(?!/\d+)
now the problem is 10.150.100.0 works fine, 10.100.4.64 subnet gets matched
with ip pattern with the following result:
10.100.4.6
Is there a workaround for this or what should change in ip regex pattern.
python script:
#!/usr/bin/env python
import re, sys
fh = 0
try:
fh = open(sys.argv[1], "r")
except IOError, message:
print "cannot open file: %s" %message
else:
for lines in fh.readlines():
lines = lines.strip()
pattIp = re.compile("(\d{1,3}(\.\d{1,3}){3})(?!/\d+)")
pattNet = re.compile("((\d{1,3})|(\d{1,3}(\.\d{1,3}){1,3}))/(\d{1,2})")
match = pattIp.search(lines)
if match is not None:
print "ipmatch: %s" %match.groups()[0]
match = pattNet.search(lines)
if match is not None:
print "subnet: %s" %match.groups()[0]
fh.close()
output with that above ip/subnet in a file
ipmatch: 10.200.0.34
ipmatch: 10.200.4.5
ipmatch: 10.178.9.45
subnet: 10.200
subnet: 10.178
ipmatch: 10.100.4.6
subnet: 10.100.4.64
subnet: 10.150.100.0
subnet: 10
TIA
Prabhu
begin:vcard
fn:Prabhu Gurumurthy
n:Gurumurthy;Prabhu
org:Silver Spring Networks;IT
adr:Suite 205;;2755 Campus Drive;San Mateo;CA;94403;USA
email;internet:[EMAIL PROTECTED]
title:Network Engineer
tel;work:(650) 357 8770 x134
tel;home:(650) 585 6527
tel;cell:(831) 224 0894
x-mozilla-html:FALSE
url:http://www.silverspringnet.com
version:2.1
end:vcard
--
http://mail.python.org/mailman/listinfo/python-list