Help with regex

John Machin sjmachin at lexicon.net
Thu Aug 6 21:27:51 EDT 2009


On Aug 7, 7:23 am, Ethan Furman <et... at stoneleaf.us> wrote:
> Nobody wrote:
> > On Thu, 06 Aug 2009 08:35:57 -0700, Robert Dailey wrote:
>
> >>I'm creating a python script that is going to try to search a text
> >>file for any text that matches my regular expression. The thing it is
> >>looking for is:
>
> >>FILEVERSION #,#,#,#
>
> >>The # symbol represents any number that can be any length 1 or
> >>greater. Example:
>
> >>FILEVERSION 1,45,10082,3
>
> >>The regex should only match the exact above. So far here's what I have
> >>come up with:
>
> >>re.compile( r'FILEVERSION (?:[0-9]+\,){3}[0-9]+' )
>
> > [0-9]+ allows any number of leading zeros, which is sometimes undesirable.
> > Using:
>
> >    (0|[1-9][0-9]*)
>
> > is more robust.
>
> You make a good point about possibly being undesirable, but I question
> the assertion that your solution is /more robust/.  If the OP
> wants/needs to match numbers even with leading zeroes your /more robust/
> version fails.

I'd go further: the OP would probably be better off matching anything
that looked vaguely like an attempt to produce what he wanted e.g.
r"FILEVERSION\s*[0-9,]{3,}" and then taking appropriate action based
on whether that matched a "strictly correct" regex.




More information about the Python-list mailing list