Regular expression

Rhodri James rhodri at wildebst.demon.co.uk
Tue Jul 21 11:39:51 EDT 2009


On Tue, 21 Jul 2009 14:30:47 +0100, Peter Fodrek <peter.fodrek at stuba.sk>  
wrote:

> Dear conference!
>
> I have  third party regular expression
>
>  self.pattern_main =  
> re.compile('(\s+|\w(?:[+])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)|
> \#\d+\=(?:[+])?\d*(?:\.\d*)?)')

Always use raw strings (r"...") when you're working with regular
expressions.  *Always*.  It may happen that you don't need them,
as in this case, because Python fails to throw an exception when
it meets unknown escape sequences in string literals, but when
you do need them you will spend hours before realising it.

Also, whoever wrote this regular expression should be taken
out and slapped with a wet fish, for it is abominable and
unlikely to look much like what you actually want.

> with code
>
>  def readline(self):
>         self.line = self.file_in.readline().rstrip()
> 	if (len(self.line)) : return True
>         else : return False
>
>  while (self.readline()):
>             words = self.pattern_main.findall(self.line)

Um.  Yes.  Well.

> This handles text file  like
>
> // remark
> PL_OFF
> PARK
> FS
> MA  52.8806 ,  18.0914
> SS
> AD_W
> PL_ON
> C   52.3955 ,  -16.1511 ,  -90.0000
>
> It handles file correctly with two exceptions
>
> 1) omits ',' in the a output

You don't do any output, so it's hard to correct that!

> 2) omits minus sign in the numbers...
>
> Would anyone recommend me what to change regular expression to add  
> thesee two
> think to the output,please?

It would help considerably if you explained what "it handles file
correctly" means, since the regular expresion makes little sense
with the input you give.  What are you trying to extract from the
input, and what do you think you are getting at the moment?

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list