【问题标题】:Python regex error: look-behind requires fixed-width patternPython regex 错误:look-behind 需要固定宽度的模式
【发布时间】:2020-02-07 03:32:26
【问题描述】:

下面的正则表达式应该匹配任何start-of-stringwhitespace: 前面的:text:,后面是end-of-stringwhitespace:(以及一些额外的规则)

我不擅长正则表达式,但我在 regexr.com 中提出了所需的解决方案:

(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)
:match1::match2: :match3:
:match4:
000:matchNot:
:matchNot:000
:match Not:

结果::match1::match2::match3::match4:

但在 Python 3 上,这会引发错误。

re.search("(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)", txt)

re.error:look-behind 需要固定宽度的模式

有人知道解决这个问题的好方法吗?任何提示都表示赞赏。

【问题讨论】:

标签: regex python-3.x


【解决方案1】:

在 python 中,您可以使用此解决方法来避免此错误:

(?:^|(?<=[\s:]))(:[^\s:]+:)(?=[\s:]|$)

锚点^$ 无论如何都是零宽度匹配器。

RegEx Demo

【讨论】:

    【解决方案2】:

    可能最简单的解决方案是使用支持无限后视的较新的regex module

    import regex as re
    
    data = """:match1::match2: :match3:
    :match4:
    000:matchNot:
    :matchNot:000
    :match Not:"""
    
    for match in re.finditer("(?<=\s|:|^)(:[^\s|:]+:)(?=\s|:|$)", data):
        print(match.group(0))
    

    这会产生

    :match1:
    :match2:
    :match3:
    :match4:
    

    【讨论】:

    • 这绝对比拆分look-behinds容易。当然,如果您可以使用其他库。
    【解决方案3】:

    另一种选择是安装regex

    $ pip3 install regex
    

    然后,我们会写一些表达式和(*SKIP)(*FAIL) 我们不希望出现的模式:

    import regex as re
    
    expression = r'(?:^\d+:[^:\r\n]+:$|^:[^:\r\n]+:\d+$|^(?!.*:\b\S+\b:).*$)(*SKIP)(*FAIL)|:[a-z0-9]+:'
    string = '''
    :match1::match2: :match3:
    :match4:
    000:matchNot:
    :matchNot:000
    :match Not:
    
    '''
    
    print(re.findall(expression, string))
    

    输出

    [':match1:', ':match2:', ':match3:', ':match4:']
    

    如果您希望简化/修改/探索表达式,在regex101.com 的右上角面板中已对此进行了说明。如果您愿意,您还可以在this link 中观看它如何与一些示例输入匹配。


    正则表达式电路

    jex.im 可视化正则表达式:

    【讨论】:

      猜你喜欢
      • 2011-02-06
      • 2018-01-10
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 2011-09-01
      • 1970-01-01
      相关资源
      最近更新 更多