newb: Simple regex problem headache

C

crybaby

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
 
C

chris.monsanto

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
 
I

Ian Clark

crybaby said:
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
 
E

Erik Jones

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®
(e-mail address removed)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top