How can I tell when a string is in fact a number?

Steve Holden sholden at holdenweb.com
Mon Nov 6 11:11:18 EST 2000


Alex Martelli wrote:
> 
> "Gaute B Strokkenes" <gs234 at cam.ac.uk> wrote in message
> news:4aem0pe3rn.fsf at kern.srcf.societies.cam.ac.uk...
> > Gaute B Strokkenes <gs234 at cam.ac.uk> writes:
> >
> > > After looking at the docs for a while, I came up with the following:
> > >
> > > def isanum(str):
> > >     import re
> > >     return re.match(r'^[0-9]', str) != None
> > >
> > > Which works beautifully.
> >
> > Whoopsie.  What I meant was:
> >
> > def isanum(str):
> >     import re
> >     return re.search(r'[^0-9]', str) == None
> 
> Heh -- it's easy to make typos, isn't it?  I made one myself,
> which is why my original RE didn't work (forgot the $ at the
> end).
> 
[snip]

And the length of the exposition (which I remembered to snip this time) is
a beautiful support of the assertion that you are better off not using
regular expressions if you can find another way to do the job!

Always-get-them-wrong-three-times-before-I-get-them-right'ly y'rs - steve
-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/





More information about the Python-list mailing list