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?
CodePudding user response:
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
Test run:
words = ['$10', '$250 million', '$200,000']
result = map(adjust_dollars, words)
print(list(result))
Output:
['10 dollars', '250 million dollars', '200,000 dollars']
CodePudding user response:
Just an example with re.sub:
t1 = "$10"
t2 = "$250 million"
t3 = "$200,000"
sub_pattern = "/$|," #Look for dollar signs or commas
tail = " dollars"
re.sub(sub_pattern,"",t1) tail -> 10 dollars
re.sub(sub_pattern,"",t2) tail -> 250 million dollars
re.sub(sub_pattern,"",t3) tail -> 200000 dollars
CodePudding user response:
Since your regex also includes symbols for Pound and Euro, I assume not all of those start with $
. Then you could use re.sub
with a callback-function to determine the currency to use. This also works if the currencies appear in the middle of the text.
import re
p = "([£\$€])\s?([,\d] (?: million| billion| trillion|))"
d = {"$": "dollars", "£": "pounds", "€": "euros"}
text = "I have $10 and £3 million and €100,000 trillion"
print(re.sub(p, lambda m: f"{m.group(2)} {d[m.group(1)]}", text))
# I have 10 dollars and 3 million pounds and 100,000 trillion euros
Also note some subtle changes to the regex: I put the currency symbols in a group, so it can be accessed later, and put the "empty" suffix at the end, otherwise it's greedily matched first and none of the others. Also, no need to put \d
twice in [...]
, and better move the space to the suffix part.
CodePudding user response:
If all of your strings start with "$" you don't need to use regex. just select them at second character with "[1:]" and add " dollars" at the end. for example if your string store in variable named a:
a[1:] " dollars"