在 Python 中使用 re 将数字替换为字符串中的占位符(包括小数和百分比)
def remove_numbers(text):
remove = re.sub(r"\W\d\S*", " [DD]", text,)
return remove
该函数在此示例字符串上运行良好。 example = “我可以给你 100,000 的 10% 给你。东西涨了 10%,所以现在要 12.25 欧元。 但如果字符串以数字开头,则第一个数字不会被占位符替换。
最佳答案
因此循环替换方法似乎是最简单的方法。
def remove_numbers(text):
nums = '123456787980'
for i in nums:
text = text.replace(i, '[DD]')
return text
关于python - 使用正则表达式删除字符串中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637106/