I'm Sure There's A Better Way

Alex Martelli aleaxit at yahoo.com
Mon Jul 9 10:38:33 EDT 2001


"Bengt Richter" <bokr at accessone.com> wrote in message
news:3b48d5b9.1400429941 at wa.news.verio.net...
    ...
> 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"
> """

That depends on your reading of the last clause.  In modern
maths, generally speaking, an empty set would be taken to
satisfy it.  Python tends to disagree, though:

>>> ''.isdigit()
0

i.e., isdigit's spec of "all the characters in the strings are
digits" is taken to imply "and there is at least one such
character".  Reverend Dodgson, AKA "Lewis Carroll", would
be happy about that -- if I understand correctly, he fought
hard (and lost) against the notion that an empty set can be
taken as satisfying any condition whatsoever (I believe he
claimed "practicality beats purity" about that, or words to
that effect:-).

So: if you take the third spec as meaning:
    "all the remaining characters, IF ANY, ..."
then '-' by itself must be taken as specifying "a correct
dollar amount", &c, and my re didn't meet the specs. If
you take the third spec as meaning:
    "all the remaining characters, AND THERE MUST BE SOME, ..."
then '-' by itself is not acceptable.  Neither is '3', though,
because there are no remaining characters apart from the
first one -- and I think my re did accept '3'.

> 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)?$' ?

I suspect a + instead of that * right in the middle might
better capture the probable intent of ambiguous spec 3
(Python-like and Dodgson-like rather than emptysettish).
But apart from that, and it's gotta be either a * or a +, I
think you got it right.


Alex






More information about the Python-list mailing list