[Tutor] IP address parse

2008-08-09 Thread Que Prime
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


Re: [Tutor] IP address parse

2008-08-09 Thread Timothy Grant
On Sat, Aug 9, 2008 at 9:57 PM, Que Prime [EMAIL PROTECTED] 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

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]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IP address parse

2008-08-09 Thread Josh Rosen


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