Results not quite what I expected

Alex Martelli aleaxit at yahoo.com
Mon Apr 2 17:40:48 EDT 2001


"Kemp Randy-W18971" <Randy.L.Kemp at motorola.com> wrote in message
news:mailman.986239804.14212.python-list at python.org...
> Alex:
>    I tried your suggestion, and I am close, but not quite.  What is
happening with the number split?
    [snip]
>     number = re.compile(r'\d+\.?d*')

As I see was pointed out already, you omitted a \ right after the ?.  Nobody
ever said
regular expression syntax is _nice_ or forgiving!-)  However, I still
suspect re's are the
simplest tool for your job (which I haven't seen precisely specified yet --
for example,
do you want 'numbers' to include an optional leading sign, a trailing
exponent, or...?).

The alternatives require you to use other tools to identify runs of
contiguous digits,
then look at non-digits right before and after digits (and right between
runs, if you
need to pick up a decimal point as a part of a 'number') to pick up various
possible
non-digit 'decorations'.  Re's offer compact ways to express this kind of
things,
although of course they do not obviate the need to specify very completely
and
precisely what you want to happen in various anomalous cases -- e.g., say
your text
has the line
    foo 23.45.67 bar
what number or numbers should be extracted from this line?  The above re,
when
corrected, will parse it as two numbers:
    23.45
    67
but IS this OK for your application, or can you somehow guarantee that no
such anomaly will ever possibly occur...?


Alex






More information about the Python-list mailing list