0

我想在一个内容中替换小数点而不是句点。

例如:

原创内容:

This is a cake. It is 1.45 dollars and 2.38 kg.

替换内容:

This is a cake. It cost 1<replace>45 dollars and 2<replace>38 kg.

如何使用 Python 正则表达式来做到这一点?

非常感谢你。

4

1 回答 1

3

re.sub与环视断言一起使用:

>>> import re
>>> s = 'This is a cake. It is 1.45 dollars and 2.38 kg.'
>>> re.sub(r'(?<=\d)\.(?=\d)', '<replace>', s)
'This is a cake. It is 1<replace>45 dollars and 2<replace>38 kg.'
于 2013-08-28T09:58:52.487 回答