python - 使用正则表达式删除字符串中的数字

标签 python

在 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/

相关文章:

python - 如何在 python 中创建自己的 map() 函数

python - 找出哪个图像具有最多的黑色

python - pandas.merge 与重合的列名

python - 为什么这段代码在没有 __init__ 方法的情况下将参数传递给构造函数?

python - 四舍五入 float ,同时保持总和相等

python - 尝试从对象转换为整数时出现无效对象错误

python - “模块”对象没有属性 'svc'

python - 解析其列少于 4 个字符的行

python - 如何在 matplotlib 中设置轴限制?

python - 运行代码时更新 kivy 小部件的属性