Re: newb: Simple regex problem headache
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® [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: newb: Simple regex problem headache
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. > > thanks > Try this: >>> import re >>> s1 =' 25000 ' >>> s2 = ' 5.5910 ' >>> num_pat = re.compile(r'([0-9]+(\.[0-9]+)?)') >>> num_pat.search(s1).group(1) '25000' >>> num_pat.search(s2).group(1) '5.5910' Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: newb: Simple regex problem headache
On Sep 21, 5:04 pm, crybaby <[EMAIL PROTECTED]> 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. > > 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
newb: Simple regex problem headache
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. thanks -- http://mail.python.org/mailman/listinfo/python-list