Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python中的列表执行re.compile()_Python_Regex - Fatal编程技术网

如何使用python中的列表执行re.compile()

如何使用python中的列表执行re.compile(),python,regex,Python,Regex,我有一个字符串列表,我想在其中筛选包含关键字的字符串 我想做一些类似的事情: fruit = re.compile('apple', 'banana', 'peach', 'plum', 'pinepple', 'kiwi'] 因此,我可以使用re.search(fruit,list\u of_strings)仅获取包含fruit的字符串,但我不确定如何使用带有re.compile的列表。有什么建议吗?(我不打算使用re.compile,但我认为正则表达式是一种很好的方法。)您需要将水果列表转

我有一个字符串列表,我想在其中筛选包含关键字的字符串

我想做一些类似的事情:

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

因此,我可以使用re.search(fruit,list\u of_strings)仅获取包含fruit的字符串,但我不确定如何使用带有re.compile的列表。有什么建议吗?(我不打算使用re.compile,但我认为正则表达式是一种很好的方法。)

您需要将水果列表转换为字符串
苹果|香蕉|桃子|李子|菠萝|猕猴桃
,以便它是一个有效的正则表达式,下面应该为您这样做:

fruit_list = ['apple', 'banana', 'peach', 'plum', 'pineapple', 'kiwi']
fruit = re.compile('|'.join(fruit_list))
edit:正如ridgerunner在评论中指出的那样,您可能希望将单词边界添加到正则表达式中,否则正则表达式将匹配
plump
之类的单词,因为它们有一个水果作为子字符串

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

您可以创建一个正则表达式,当找到以下任一术语时,该正则表达式将匹配:

>>> 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
s,t=“请给我一个猕猴桃。”,“有人要草莓吗?” >>>进口稀土 >>>pattern=re.compile('苹果|香蕉|桃|李子|菠萝|猕猴桃',re.IGNORECASE) >>>模式。搜索 >>>模式。搜索(t)#什么也找不到 代码:

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的正则表达式改进fruit_测试,如下所示

fruit_test = lambda x: andrew_re.search(x) is None
由于您需要精确匹配,因此不需要regex 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']
Pyhton 3.x更新:

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

或者,如果您需要字符串:return[str for str in fruits\u vegies if fruits\u test(str)]在这种情况下,regex比执行几个单独的子字符串测试更有效。@安德鲁:取决于水果和句子的数量,即便如此,我们还是在几毫秒内讨论了2倍。@hop-我很有信心,无论结果或句子的数量如何,正则表达式都会更快。使用regex,您还可以访问匹配的水果。@Andrew:Re-efficiency:noted。重新访问匹配项:这很容易,请检查我的更新。@Andrew:我不会质疑正则表达式的速度更快,但非正则表达式的解决方案在小数据集上可能就足够了,而且更容易理解,特别是当您在正则表达式方面遇到问题时。+1但我会添加这样的单词边界:
fruit=Re.compile('\\b(?:'+'''.''.join(fruit\u list+)\\b'))
@ridgerunner-很好!事实上,现在在字符串中写“菠萝”的方式将始终与“苹果”匹配,为我的答案添加单词边界。@user808545-没问题,单击我答案旁边复选标记的轮廓,将其标记为可接受的解决方案。有效,+1。如果我更新了你的一些答案,请不要惊慌,在这个月的回答中休息一下,并利用时间阅读一些旧的东西。根据字符串列表的内容,你可能需要tp escape它们:fruit=re.compile(r'\b(?:%s)\b'%'|'。join([re.escape(x)表示fruit\u列表中的x]))