【问题标题】:Convert string to compiled regex matching the exact string将字符串转换为与确切字符串匹配的已编译正则表达式
【发布时间】:2013-12-10 18:42:35
【问题描述】:

有没有一种简单的方法可以将精确字符串匹配为re.compile 对象?例如,我想混合精确的字符串和正则表达式。

【问题讨论】:

    标签: python regex python-3.x


    【解决方案1】:

    您可能可以使用re.escape 转义字符串中的所有内容,然后将转义的字符串用作模式。

    def transform(s):
        return re.compile(re.escape(s))
    

    【讨论】:

    • 我感到很惭愧。如此简单...谢谢!
    猜你喜欢
    • 2022-01-17
    • 2020-08-22
    • 2019-06-23
    • 2020-12-17
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多