I'm making a program that takes currency from a string and converts it in to other currencies. For example, if the string was 'the car cost me $13,250'
I would need to get $
and 13250
. I have this regex already (?:\£|\$|\€)(?:.{1,})
that sort of does it, however there is a reasonably large possibility that the string might have more than one price, all using different currencies. This is something that I do no know how to do effectively.
What I need to know is how to extract all of the prices from a string. I think even if the regex just returns something like ['$12,250,000','£14,500,123','£120.25']
then it is fine because I can use something like this to get the number:
prices = ['$12,250','£14,500','£120']
for value in prices:
value.replace(',','')
And something like this to get the currency:
for c in prices:
currency = c[0]
Then there is the problem that the price might not be a whole number, and might be something like $12.54
. Any help on how to get that initial list of prices would be great.
This regular expression will work better for your purposes:
(?:[\£\$\€]{1}[,\d]+.?\d*)
Try it out here.
Then as sainoba notes, you can use re.findall
or re.finditer
to get the matches.
Then you can extract the currency from the first character, remove commas, and finally split on a decimal point if needed.
When dealing with currencies, you cannot use simple approaches like replacing commas and periods. There are a multitude of language and regional differences. The Euro may use commas or periods as the decimal separator. Some locales may have two or three digits between grouping separators. The currency symbol may be on the left or right. A symbol may represent any one of a dozen different currencies, depending on the user's locale.
Make use of a library to handle this work for you. This issue has been discussed in detail in other posts, such as this one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With