我是RegEx新手,仍然接受模式匹配。但我想了解模式替换。我希望更改句子中的货币模式,其中值可以是任何值且不可预测的,但总是采用以下格式:
<currency_symbol><number><number><dot><number><number><letter>
例如:
'mr x is worth $44.4m and mr y is worth $59.1m'
为:
'mr x is worth $44400000 and mr y is worth $59100000'
我设法匹配模式,但没有替换:
>>> import re
>>> sent = "mr x is worth $44.4m and mr y is worth $59.1m"
>>> print(re.findall(r'\$\d+\.\d+\m', sent))
['$44.4m', '$59.1m']
如何实现正则表达式模式替换?还是有比正则表达更好的方法?
答案 0 :(得分:3)
像这样进行替换的最简单方法是使用re.sub
repl
函数:
>>> import re
>>> source = 'mr x is worth $44.4m and mr y is worth $59.1m'
>>> def sub_func(match):
"""Convert the match to the new format."""
string = match.group(0)
millions = int(float(string[1:-1]) * 1000000)
return '${:d}'.format(millions)
>>> re.sub(r'\$\d+\.\d+m', sub_func, source)
'mr x is worth $44400000 and mr y is worth $59100000'
您可以使用'${:,d}'.format(millions)
来获取,例如'$44,400,000'
。