On Sep 21, 5:04 pm, crybaby <[EMAIL PROTECTED]> wrote:
> import re
>
> s1 ='&nbsp;25000&nbsp;'
> s2 = '&nbsp;5.5910&nbsp;'
>
> 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 ='&nbsp;25000&nbsp;'
> 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 = '&nbsp;5.5910&nbsp;'
> using regex pattern, mypat = re.compile('[0-9]*(\.[0-9]*|$)').  mypat
> works fine for real numbers, but doesn't work for whole numbers.
>
> thanks

Your pattern matches the empty string a bit too well, if you know what
I mean!

Changing the regex slightly to '[0-9]+(\.[0-9]+)?' yields the results
you want:

25000
5.5910
25000.0


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to