[Tutor] IP address parse

Timothy Grant timothy.grant at gmail.com
Sun Aug 10 07:46:53 CEST 2008


On Sat, Aug 9, 2008 at 9:57 PM, Que Prime <queprime at gmail.com> 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor

Well there's one glaring problem, but I'll put that down to a
re-keying error. there are no quote marks around your pattern string.

However, the likely problem is that you are using re.match() instead
of re.search().

Your re will ONLY match if the only thing on the line matches your pattern.

-- 
Stand Fast,
tjg.  [Timothy Grant]


More information about the Tutor mailing list