[Tutor] trouble with re

Ertl, John john.ertl at fnmoc.navy.mil
Mon May 8 20:00:25 CEST 2006


Kent,

Thanks for the nock on the head,  that has bitten me before.  Taking out the
spaces worked great.

Thanks again,

John Ertl 

 -----Original Message-----
From: 	tutor-bounces at python.org [mailto:tutor-bounces at python.org]  On
Behalf Of Kent Johnson
Sent:	Monday, May 08, 2006 10:53 AM
Cc:	tutor at python.org
Subject:	Re: [Tutor] trouble with re

Ertl, John wrote:
> I have a file with 10,000 + lines and it has a coma delimited string on
each
> line.
> 
> The file should look like:
> 
> DFRE,ship name,1234567
> FGDE,ship 2,
> ,sdfsf
> 
> The ,sdfsf  line is bad data
> 
> p = re.compile('\d{7}$ | [,]$')   # this is the line that I can not get
> correct I an trying to find lines that end in a comma or 7 digits

Spaces are significant in regular expressions unless you compile them 
with the re.VERBOSE flag. Also you don't need to make a group for a 
single character. Try
p = re.compile('\d{7}$|,$')
or maybe
p = re.compile('(\d{7}|,)$')

Actually since the seven digits are preceded by the comma you could just 
make the digits optional:
p = re.compile(',(\d{7})?$')

Kent

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list