【问题标题】:Python: Regex matches in Windows but not in Linux 4.4.59+Python:正则表达式在 Windows 中匹配,但在 Linux 4.4.59+ 中不匹配
【发布时间】:2020-10-02 16:17:21
【问题描述】:

我有这段代码,试图用直引号替换大引号

        quoteChars = [u'\u2018', u'\u2019']
        pattern = u'({0})'.format('|'.join(quoteChars))
        matched = re.search(pattern, myString)  # match against whole string
        if matched:
            self.log('SELF:: Search Query:: Replacing characters in string. Found one of these {0}'.format(pattern))
            myString = re.sub(pattern, "'", myString)
            self.log('SELF:: Amended Search Query [{0}]'.format(myString))
        else:
            self.log('SELF:: Search Query:: String has none of these {0}'.format(pattern))

我将变量 myString 设置为以下(‘Pop‑Up’ Edition) 在 Windows 中,它正确检测到有撇号,但是当我在 Mac 上尝试它时,它报告其操作系统为 Linux 4.4.59+,它与模式不匹配..

我必须在 Linux 上设置不同的正则表达式模式吗?规则是什么?与单引号和双引号的关系,打开还是关闭?

【问题讨论】:

    标签: regex linux windows macos python-2.7


    【解决方案1】:

    我会使用正则表达式转义:

    quoteChars = [r'\u2018', r'\u2019']
    pattern = '|'.join(quoteChars)
    

    然后

    myString = re.sub(pattern, "'", myString)
    

    【讨论】:

    • 这不起作用它搜索 '\u2018' 作为 6 个字符,而不是它应该是什么 unicode 字符......
    • @JasonHudson 但是,这些字符是removed
    • 在windows上不行...我会请我在Linux上的朋友测试...我之前的代码在Windows上可以运行...为什么它不能在Linux下运行?
    • @JasonHudson 如果您使用 Python 2.x,我的解决方案仍然有效,请参阅 ideone.com/HPrMaA。这是在 Linux 中。
    • 我会尝试新代码...我从不知道可以将 u 和 r 结合起来...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多