I tried to teach myself Python and am currently on regular expressions. The training text I use seems to be aimed at teaching Perl or some other language that is not Python, so I had to adapt expressions to Python a bit. However, I am not very experienced, and I fell into the trap of trying to get the expression to work.
The problem is finding text for price instances expressed either without decimals, $ 500, or with decimals, $ 500.10.
Here is what the text recommends:
\$[0-9]+(\.[0-9][0-9])?
Replicating the text, I use this code:
import re inputstring = "$500.01" result = re.findall( r'\$[0-9]+(\.[0-9][0-9])?', inputstring) if result: print(result) else: print("No match.")
However, the result is not equal to $ 500.01, but rather:
.01
I find it strange. If I remove the parentheses and the optional decimal part, it works fine. So using this:
\$[0-9]+\.[0-9][0-9]
I get:
$500.01
How to get a regular expression to return values ββwith decimal parts and without them?
Thanks.
source share