Skip to content Skip to sidebar Skip to footer

Python Regex To Match Currency With Or Without Comma Or Decimal

How to write regex that can 1. match currency which may or may not include a comma or decimal and 2. match the currency code only. What I see is mostly matching currency symbols. I

Solution 1:

Get the Currency:

Having an exhaustive list of valid currencies might not be feasible but if it is a limited number of currencies then you can do that like this:

re.findall('USD|CAD','USD1 USD1.00 USD100.00 USD1,000 CAD1,000.00 123XXX123')

Output:

['USD', 'USD', 'USD', 'USD', 'CAD']

Get the amount:

Using capturing group, re.findall returns each capture in a tuple. Using a non-capturing group (?:) will solve the issue.

re.findall('(?<=USD|CAD)\d{1,3}(?:,\d{3})*(?:\.\d+)?(?=\s)','Userid 9XXXX219 sales USD300,000.00 On 01-JUL-2016 08:34:32')

Output:

['300,000.00']

Illustration with the example text:

re.findall('(?<=USD|CAD)\d{1,3}(?:,\d{3})*(?:\.\d+)?(?=\s)','USD1 USD1.00 USD100.00 USD1,000 CAD1,000.00 123XXX123')

Output:

['1', '1.00', '100.00', '1,000', '1,000.00']

Read more the following here:

(?=) - positive lookahead (?<=) - positive lookbehind

Solution 2:

To match currency only you can use : (\d[0-9,.]+)

and to match currency codes you can use : ([A-Z]+)

Demo and Explaination

Solution 3:

'\d+([.,]?\d*)*' that should match all cases. If you want, you can also add space. Like this:

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

For the currency codes: '[A-Z]{3}' should work.

P.S. As per SilentMonk suggestion for the non-capture groups:

(?:[A-Z]{3})(?:\d+(?:[.,]?\d*)*)

Post a Comment for "Python Regex To Match Currency With Or Without Comma Or Decimal"