On Aug 9, 2008, at 10:46 PM, Josh Rosen wrote:
There are a few different problems in your code. First off, regular
expressions must be passed to re.compile() as strings.
patt = re.compile(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.
(\[0-9]{1,3})
should read
patt = re.compile(r"(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.
(\[0-9]{1,3})").
I've used a raw string literal here to prevent Python from
interpreting the backslashes as character escapes. However, this
regular expression still won't work. If you're going to use a
character class, there's no need to put a backslash in front of it.
Correcting this, the line becomes:
patt = re.compile(r"([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]
{1,3})")
This works, but it can be made simpler by using the shorthand
notation \d in place of [0-9]:
patt = re.compile(r"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})")
Since it doesn't look like you're doing anything with the parts of
the ip besides writing the whole ip to a file, you can eliminate the
capturing parentheses in your regular expression and replace them
with a single pair:
patt = re.compile(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")
The string formatting expression becomes:
outfile.write("%s\n" % m.groups())
Hope this helps,
Josh
On Aug 9, 2008, at 9:57 PM, Que Prime wrote:
I'm trying to parse a log file for all ip addresses but can't get
my RE to work. Thanks in advance for pointing me in the right
direction
#IP address parse
##############################
import re
infile = open("host0_declare.txt","r")
outfile = open("out.txt","w")
patt = re.compile(\[0-9]{1,3})\.(\[0-9]{1,3})\.(\[0-9]{1,3})\.
(\[0-9]{1,3})
for line in infile:
m = patt.match(line)
if m:
outfile.write("%s.%s.%s.%s\n"%m.groups())
infile.close()
outfile.close()
#############################
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor