【问题标题】:how to do re.compile() with a list in python如何在 python 中使用列表执行 re.compile()
【发布时间】:2011-10-08 16:17:12
【问题描述】:

我有一个字符串列表,我想在其中过滤包含关键字的字符串。

我想做这样的事情:

fruit = re.compile('apple', 'banana', 'peach', 'plum', 'pinepple', 'kiwi']

所以我可以使用 re.search(fruit, list_of_strings) 仅获取包含水果的字符串,但我不确定如何将列表与 re.compile 一起使用。有什么建议? (我不打算使用 re.compile,但我认为正则表达式会是一个很好的方法。)

【问题讨论】:

    标签: python regex


    【解决方案1】:

    您需要将水果列表转换为字符串apple|banana|peach|plum|pineapple|kiwi,使其成为有效的正则表达式,以下内容应为您执行此操作:

    fruit_list = ['apple', 'banana', 'peach', 'plum', 'pineapple', 'kiwi']
    fruit = re.compile('|'.join(fruit_list))
    

    edit:正如 ridgerunner 在 cmets 中指出的那样,您可能希望在正则表达式中添加单词边界,否则正则表达式将匹配 plump 这样的单词,因为它们有一个水果作为子字符串.

    fruit = re.compile(r'\b(?:%s)\b' % '|'.join(fruit_list))
    

    【讨论】:

    • +1 但我会像这样添加单词边界:fruit = re.compile('\\b(?:'+ '|'.join(fruit_list +')\\b'))
    • @ridgerunner - 好点!事实上,现在字符串中“菠萝”的写法总是匹配“苹果”,为我的答案添加了单词边界。
    • @user808545 - 没问题,点击我的答案旁边的复选标记的轮廓,将其标记为已接受的解决方案。
    • 高效,+1。如果我对你的一些答案投了赞成票,请不要惊慌,在本月的回答中休息一下,并利用时间阅读一些旧的东西。
    • 根据您的字符串列表,您可能需要 tp 转义它们:fruit = re.compile(r'\b(?:%s)\b' % '|'.join([ re.escape(x) for x in fruit_list]))
    【解决方案2】:

    您可以创建一个正则表达式,它会在找到任何术语时匹配:

    >>> s, t = "A kiwi, please.", "Strawberry anyone?"
    >>> import re
    >>> pattern = re.compile('apple|banana|peach|plum|pineapple|kiwi', re.IGNORECASE)
    >>> pattern.search(s)
    <_sre.SRE_Match object at 0x10046d4a8>
    >>> pattern.search(t) # won't find anything
    

    【讨论】:

      【解决方案3】:

      代码:

      fruits =  ['apple', 'banana', 'peach', 'plum', 'pinepple', 'kiwi'] 
      fruit_re = [re.compile(fruit) for fruit in fruits]
      fruit_test = lambda x: any([pattern.search(x) for pattern in fruit_re])
      

      示例用法:

      fruits_veggies = ['this is an apple', 'this is a tomato']
      return [fruit_test(str) for str in fruits_veggies]
      

      编辑:我意识到 Andrew 的解决方案更好。您可以使用 Andrew 的正则表达式改进fruit_test

      fruit_test = lambda x: andrew_re.search(x) is None
      

      【讨论】:

      • 或者如果你需要字符串:return [str for str in fruits_veggies if fruit_test(str)]
      【解决方案4】:

      如你所愿,exact 匹配,不需要 正规表达式 imo...

      fruits = ['apple', 'cherry']
      sentences = ['green apple', 'yellow car', 'red cherry']
      for s in sentences:
          if any(f in s for f in fruits):
              print s, 'contains a fruit!'
      # green apple contains a fruit!
      # red cherry contains a fruit!
      

      编辑:如果您需要访问匹配的字符串:

      from itertools import compress
      
      fruits = ['apple', 'banana', 'cherry']
      s = 'green apple and red cherry'
      
      list(compress(fruits, (f in s for f in fruits)))
      # ['apple', 'cherry']
      

      【讨论】:

      • 在这种情况下,正则表达式比做几个单独的子字符串测试更有效。
      • @Andrew:取决于水果和句子的数量,即便如此我们在几毫秒内谈论 2x。
      • @hop - 我非常有信心正则表达式会更快,无论水果或句子的数量如何。使用正则表达式,您还可以访问匹配的水果。
      • @Andrew:重新效率:已注明。重新访问比赛:很简单,检查我的更新。
      • @Andrew:我不会质疑正则表达式更快,但非正则表达式解决方案可能足以处理小型数据集并且更易于理解,特别是如果您在使用正则表达式时遇到问题。
      【解决方案5】:

      Pyhton 3.x 更新:

      fruit_list = ['apple', 'banana', 'peach', 'plum', 'pineapple', 'kiwi']
      fruit = re.compile(r'\b(?:{0})\b'.format('|'.join(fruit_list))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-01
        • 1970-01-01
        • 2018-04-26
        • 2016-11-17
        • 2010-10-01
        • 1970-01-01
        相关资源
        最近更新 更多