I'm Sure There's A Better Way
Bengt Richter
bokr at accessone.com
Sun Jul 8 18:14:51 EDT 2001
On Sun, 8 Jul 2001 09:40:21 +0200, "Alex Martelli" <aleaxit at yahoo.com>
wrote:
>"Bengt Richter" <bokr at accessone.com> wrote in message
>news:3b478576.1314346961 at wa.news.verio.net...
> ...
>> >> dollar amount. Which means it follows these rules:
>> >>
>> >> First character is numeric or "-"
> ...
>> >This is very easy to express as a Regular Expression:
>> > r'-?\d+(\.\d\d)?$'
>> or
>> r'^-?(\d*\.\d\d|\d+)$'
>> if you want to allow .12 (or -.00), see below ;-)
>
>Yes, but that follows a very different spec from the one
>given by the original poster, where the first rule was, as
>above quoted, 'first character is numeric or "-"'.
>
>Getting the specs 'right' for a fuzzily defined domain is
>challenging, I was just trying to help implement the
>specs the poster thought were right:-).
>
>
Well, ok, but did either of us really nail that? ;-)
"""
First character is numeric or "-"
At most, one "." is allowed and, if present, is followed by exactly
two digits
All the remaining characters must be in the range "0" - "9"
"""
I missed because I allowed \.\d\d by itself, which has first
character '.' not equal to '-' or digit.
But according to the rules, ISTM you missed too, since you
don't allow -\.\d\d, which has an ok first character. The rules
don't say whether any digits must precede a '.'
In fact, we probably both missed by not letting '-' by itself
be legal ;-)
Perhaps r'(-|\d)\d*(\.\d\d)?$' ?
More information about the Python-list
mailing list