newb: Simple regex problem headache
Erik Jones
erik at myemma.com
Fri Sep 21 17:18:26 EDT 2007
On Sep 21, 2007, at 4:04 PM, crybaby wrote:
> import re
>
> s1 =' 25000 '
> s2 = ' 5.5910 '
>
> mypat = re.compile('[0-9]*(\.[0-9]*|$)')
> rate= mypat.search(s1)
> print rate.group()
>
> rate=mypat.search(s2)
> print rate.group()
> rate = mypat.search(s1)
> price = float(rate.group())
> print price
>
> I get an error when it hits the whole number, that is in this format:
> s1 =' 25000 '
> For whole number s2, mypat catching empty string. I want it to give
> me 25000.
> I am getting this error:
>
> price = float(rate.group())
> ValueError: empty string for float()
>
> Anyone knows, how I can get 25000 out of s2 = ' 5.5910 '
> using regex pattern, mypat = re.compile('[0-9]*(\.[0-9]*|$)'). mypat
> works fine for real numbers, but doesn't work for whole numbers.
I'm not sure what you just said makes a lot of sense, but if all your
looking for is a regex that will match number strings with or without
a decimal point, try '\d*\.?\d*'
Erik Jones
Software Developer | Emma®
erik at myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)
Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
More information about the Python-list
mailing list