Python3 正则处理特殊字符

版权声明:欢迎任何形式的转载,但请务必注明出处,共创知识服务 https://blog.csdn.net/ASUKA2020/article/details/83819582

Python3 正则处理特殊字符

import re

# 测试文本
content = '<h1>你好</h1>666*Notice*\toh\rsee\ngood&nbsp;'

def clear_special_char(content):
    '''
    正则处理特殊字符
    :param content:原文本
    :return: 清除后的文本
    '''
    s = re.sub(r"</?(.+?)>|&nbsp;|\t|\r", "", content)
    s = re.sub(r"\n", " ", s)
    s = re.sub(r"\*", "\\*", s)

    return s

# 调用
print(clear_special_char(s))
# 打印结果:你好666\*Notice\*ohsee good

猜你喜欢

转载自blog.csdn.net/ASUKA2020/article/details/83819582