【问题标题】:Wrong result with regular expressions正则表达式的错误结果
【发布时间】:2021-05-25 16:04:22
【问题描述】:

知道为什么下面的正则表达式会削减句子的“fl”部分吗?

re.sub('[^a-zA-Z]', ' ', '\nFor a this river, the flow becomes complicated in the floodplain')

这是我得到的结果:

'For a this river  the  ow becomes complicated in the  oodplain'

【问题讨论】:

    标签: python python-3.x regex


    【解决方案1】:

    您正在用空格替换所有非字母字符。

    在您的代码中,“fl”实际上是 - 单个 unicode(非 AZ)字符,因此它与标点符号一起被删除。

    【讨论】:

    • OK.. 它为什么将“fl”视为单个 unicode 非 AZ 字符,而不是“th”(在此)?
    • 这个有问题的字符是'latin small ligature fl'。这不是A-Za-z 中的任何字符,就像'2' 或'@' 也不是:fileformat.info/info/unicode/char/fb02/index.htm
    猜你喜欢
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多