Question on regex
Prabhu Gurumurthy
pgurumur at gmail.com
Sat Dec 23 06:32:32 EST 2006
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pgurumur.vcf
Type: text/x-vcard
Size: 369 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20061223/8ddbb385/attachment.vcf>
More information about the Python-list
mailing list