【问题标题】:Python regex list using list使用列表的 Python 正则表达式列表
【发布时间】:2013-11-24 15:03:13
【问题描述】:

好的,所以我有一个字符串列表,可以用作正则表达式搜索。例如

import re
regex_strings = ['test1','test2','test3']

#Obviously this won't work here as is!  
regex = re.compile(regex_strings)

我还有另一个字符串列表。例如

strgs = ['This is a test1','This is a test2','This is a test1','This is a test1','This is a test3']

我想遍历 'strgs' 列表,并根据 'regex_strings' 列表正则表达式检查每个字符串。然后,如果有匹配,则返回整个字符串。

我已经在这里摸不着头脑了,我不太确定解决这个问题的最佳方法。任何建议将不胜感激!

问候。

【问题讨论】:

    标签: python regex list


    【解决方案1】:

    您可以像这样在正则表达式中使用| 运算符

    re.compile("(" + "|".join(regex_strings) + ")")
    

    所以,正则表达式变成了这样(test1|test2|test3)。你可以在这里查看这个正则表达式的含义http://regex101.com/r/pR5pU1

    示例运行:

    import re
    regex_strings = ['test1','test2','test3']
    regex = re.compile("(" + "|".join(regex_strings) + ")")
    strgs = ['This is a test1','This is a test2','This is a test1','This is a test1','This is a test3']
    print [strg for strg in strgs if regex.search(strg)]
    

    输出

    ['This is a test1', 'This is a test2', 'This is a test1', 'This is a test1', 'This is a test3']
    

    编辑:如果你只想返回匹配的部分,

    import re
    regex_strings = ['test1','test2','test3']
    regex = re.compile("(" + "|".join(regex_strings) + ")")
    strgs = ['This is a test1','This is a test2','This is a test1','This is a test1','This is a test3']
    result = []
    for strg in strgs:
        temp = regex.search(strg)
        if temp:
            result.append(temp.group())
    print result
    

    输出

    ['test1', 'test2', 'test1', 'test1', 'test3']
    

    【讨论】:

    • 非常感谢。它奏效了。虽然正如我在下面提到的,我需要花一些时间来了解发生了什么。
    • @user1513388 不客气。不明白的功能请翻阅文档,玩玩。如果您仍有疑问,请在此处发表评论。我会尽力帮助你:)
    • 只是一个简单的问题 - 如果我想返回实际匹配项而不是整行,例如测试 1、测试 2 或测试 3。我怎么能这样做?
    • @user1513388 请立即查看我的答案。
    • 完美!再次感谢您的示例。
    【解决方案2】:

    如果不是太多数据并且你的正则表达式不需要编译,这行就可以了。

    print [ s for s in strgs for reg in regex_strings if re.search(reg, s) ]
    

    否则,也许这会有所帮助:

    import re
    compiled_regs = map(re.compile, regex_strings)
    print [ s for s in strgs for reg in compiled_regs if reg.search(s) ]
    

    两种情况下的输出:

    ['This is a test1', 'This is a test2', 'This is a test1', 'This is a test1', 'This is a test3']
    

    【讨论】:

      【解决方案3】:

      有更好的方法可以做到这一点,其他答案是这种方法的好例子,但我想我会从头开始

      让我们逐步考虑这个问题。现在不需要编译,所以让我们跳过它。

      您想遍历 strgs 并检查每个字符串。这给我们留下了。

      for string in strgs:
          check it against each string in regex_string
      

      显然扩展为

      for string in strgs:
          for regex_string in regex_strings:
             check string against regex_string and print if matching
      

      现在唯一的问题是,如何根据正则表达式检查字符串。通过谷歌快速浏览一下这个页面http://docs.python.org/2/howto/regex.html,或者

      re.match(regex_string, string)
      

      包括这个给出

      for strg in strgs:
          for regex_string in regex_strings:
             m = re.match(regex_string, strg)
             if m: #short for if m != None
                 print value of m
      

      回到正则表达式 howto 给我们 m.string 留下完整的代码

      for strg in strgs:
          for regex_string in regex_strings:
             m = re.match(regex_string, strg)
             if m: #short for if m != None
                 print m.string
      

      完成这些步骤后,添加正则表达式的编译并不难,所以我把它留给你。

      【讨论】:

      • 哇! - 感谢您提供有关其工作原理的详细概述。当我有更多时间时,我会再次提到这一点。与此同时,@thefourtheye 的回答确实有效,但并不完全理解
      猜你喜欢
      • 2012-09-02
      • 2016-09-27
      • 2019-04-01
      • 1970-01-01
      • 2019-07-04
      • 2013-02-07
      • 2020-10-15
      • 2023-03-08
      • 2015-08-02
      相关资源
      最近更新 更多