【问题标题】:match all words not starting with ':' Python regex匹配所有不以 ':' Python 正则表达式开头的单词
【发布时间】:2014-07-02 10:49:36
【问题描述】:

您好,我需要从 :ca:cr:pr cola xx 匹配 cola xx,但也能够在没有出现 ca:cr:pr 时获得 cola xx。以:开头的标签数量可以不同,长度也可以不同。

>>> string
':ca:cr:pr cola xx'
>>> re.findall("\w+", string)
['ca', 'cr', 'pr', 'cola', 'xx']
>>> re.findall(":\w+", string)
[':ca', ':cr', ':pr']
>>> re.findall("^(:\w+)", string)
[':ca']

我也尝试使用lookbehinds (http://runnable.com/Uqc1Tqv_MVNfAAGN/lookahead-and-lookbehind-in-regular-expressions-in-python-for-regex) 但不安全。

>>> re.findall(r"(\s\w+)(?!:)",string)
[' cola', ' xx']
>>> string="cola"
>>> re.findall(r"(\s\w+)(?!:)",string)
[]

也就是没有标签的时候,只有cola是检测不到的。

如何改进我的正则表达式以按预期工作?

再举一个例子:

:c cola xx -> cola xx

:ca:c cola xx -> cola xx

:ca:cr:pr cola xx -> cola xx

cola xx -> cola xx

cola -> cola

【问题讨论】:

    标签: python regex lookbehind


    【解决方案1】:

    如果我正确理解您的要求,我相信这样的事情应该可行:

    (?<!:)\b\w+
    

    regex101 demo

    在代码中:

    results = re.findall(r'(?<!:)\b\w+', string)
    

    【讨论】:

      【解决方案2】:

      为什么不将所有以冒号开头的单词全部替换掉​​?

      result = re.sub(r":\w+\b", "", subject)
      

      【讨论】:

        【解决方案3】:

        希望这会奏效

        re.findall("(?<!:)(\w+)", string)
        

        【讨论】:

        【解决方案4】:

        我会这样做:

        (?<!:)\w+(?:\s\w+)?
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多