How to replace "$10" with "10 dollars" using regex?
I have some phrases like below:
This is not my spending '$10', this is companys spending: '$250 million' and this is some other figure: '$200,000'.
that I would like to remove the dollar symbols and add "dollar" at the end of phrase, like this:
This is not my spending '10 dollars', this is companys spending: '250 million dollars' and this is some other figure: '200000 dollars'.
I now have regex to match(
[£\$€][\s\d,\d]+(|million|billion|trillion)), but I haven’t been able to get the substitution part right.
How do I do this?
You may use the following function to achieve what you described.
import re def adjust_dollars(text): text = re.sub(r'^\$', '', text) text = re.sub(r'(.$)', r'\1 dollars', text) return text
words = ['$10', '$250 million', '$200,000'] result = map(adjust_dollars, words) print(list(result))
['10 dollars', '250 million dollars', '200,000 dollars']
Answered By – Slybot