How can I achieve this without using if operator and huge multiline constructions?
Example:
around $98 million last week
above $10 million next month
about €5 billion past year
after £1 billion this day
Convert to
around 98 million dollars last week
above 10 million dollars next month
about 5 billion euros past year
after 1 billion pounds this day
CodePudding user response:
You probably have more cases than this so the regular expression may be too specific if you have more cases, but re.sub
can be used with a function to process each match and make the correct replacement. Below solves for the cases provided:
import re
text = '''\
around $98 million last week
above $10 million next month
about €5 billion past year
after £1 billion this day
'''
currency_name = {'$':'dollars', '€':'euros', '£':'pounds'}
def replacement(match):
# group 2 is the digits and million/billon,
# tack on the currency type afterward using a lookup dictionary
return f'{match.group(2)} {currency_name[match.group(1)]}'
# capture the symbol followed by digits and million/billion
print(re.sub(r'([$€£])(\d [mb]illion)\b', replacement, text))
Output:
around 98 million dollars last week
above 10 million dollars next month
about 5 billion euros past year
after 1 billion pounds this day
CodePudding user response:
You could make a dictionary mapping currency symbol to name, and then generate the regexes. Note that these regexes will only work for something in the form of a number and then a word.
import re
CURRENCIES = {
r"\$": "dollars", # Note the slash; $ is a special regex character
"€": "euros",
"£": "pounds",
}
REGEXES = []
for symbol, name in CURRENCIES.items():
REGEXES.append((re.compile(rf"{symbol}(\d [^\W\d_] )"), rf"\1 {name}"))
text = """around $98 million last week
above $10 million next month
about €5 billion past year
after £1 billion this day"""
for regex, replacement in REGEXES:
text = regex.sub(replacement, text)
CodePudding user response:
It's useful in a case like this to remember that re.sub
can accept a lambda rather than just a string.
The following requires Python 3.8 for the :=
operator.
s = "about €5 billion past year"
re.sub(r'([€$])(\d )\s ([mb]illion)',
lambda m: f"{(g := m.groups())[1]} {g[2]} {'euros' if g[0] == '€' else 'dollars'}",
s)
# 'about 5 billion euros past year'
CodePudding user response:
this question was partially solved in different questions:
My suggestions:
string = "around $98 million last week"
new_string = re.sub(r"[^a-zA-Z0-9]"," ",string)
print(new_string)