Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

python get float numbers in reqular expression

I have the possibilities of these formats:

1,34$
1.34$
114$
0,34$
0.34$

and i want to get just the number .

i used this regular express

(\d+),(\d*)

but when i test it in 1,34$ i got two results, which are 1 and 34

please I need to use just regular expression

obviously I am not handeling the case of dot and comma because I didn't know how to do that

my question is who to get the whole number

my second question is how can i not care if it is comma or dot.

thanks

Upvotes: 0

Views: 334

Answers (1)

isedev
isedev

Reputation: 19601

The correct regular expression would be:

(\d+(?:[.,]\d*)?)

Which means: one or more digits optionally followed by a dot or a comma, followed by zero or more digits.

The capture group is the entire expression (everything between parentheses). Note that (?...) is not a capture group, simply an expression grouping so that ? applies to everything in that group.

The reason why you were getting two results is that you were using two capture groups.

If you then want to convert the result into a number, you will have to deal with replace the possible comma with a dot and convert to a float. Assuming the captured text is in text:

float(text.replace(',','.'))

For example:

import re
text = '1,32$'
match = re.match(r'(\d+(?:[.,]\d*)?)',text)
float(match.group(1).replace(',','.'))
>>> 1.320

Upvotes: 1

Related Questions