简体   繁体   English

如何使用正则表达式找出Python列表中是否有任何一项在字符串中?

[英]How can I find out if any one item in a Python list is in a string using regex?

I have a list and I'd like to find out if any one item in that list is in a string using regex. 我有一个列表,我想使用正则表达式找出该列表中是否有任何一项包含在字符串中。 Is there a way to do this? 有没有办法做到这一点?

Sure. 当然。

myregex = re.compile(...)
print any(myregex.search(s) for s in my_list_of_strings)

or maybe: 或者可能:

regexs = [re.compile(s) for s in my_list_of_regex_strings]
any(r.search(my_string) for r in regexs)

which I suppose is probably the same thing as : 我想可能与:

regex_str = '|'.join('(?:%s)'%re.escape(s) for s in list_of_regex_strings)
re.search(regex_str,my_string)

I still can't tell which way you're trying to go with this... 我仍然无法确定您要采用哪种方式...

Finally, if you actually want to know which regex matched: 最后,如果您实际上想知道哪个正则表达式匹配:

next(regex_str for regex_str in regex_str_list if re.search(regex_str,mystring))

This will raise a StopIteration exception (that you can catch) if none of the regex's match. 如果没有一个正则表达式匹配项,将引发StopIteration异常(您可以捕获)。

I'm assuming that OP is asking how to to find out if any one item in a list of strings matches a pattern using regex . 我假设OP正在询问如何使用regex查找字符串列表中的任何一项是否与模式匹配

# import the regex module
import re

# some sample lists of strings
list1 = ['Now', 'is', 'the', 'time', 'for', 'all', 'good', 'men']
list2 = ['Me', 'Myself', 'I']

# a regex pattern to match against (looks for words with internal vowels)
pattern = '.+[aeiou].+'

# use any() around a list comprehension to determine
# if any match via the re.match() function
any(re.match(pattern, each) for each in list1)

# if you're curious to determine just what is matching your expression, use filter()
list(filter(lambda each: re.match(pattern, each) , list2))
for each item in list:
   use regex on string

thats as specific as possible given the generalistic nature of your question. 鉴于您的问题的一般性,多数意见应尽可能具体。

EDIT: this is psuedocode, not python 编辑:这是psuedocode,而不是python

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何找到一个字符串,其中包含两个正则表达式之间的任意字符串,但python中的某个正则表达式除外? - How can I find a string that contains any between two regular expressions except for a certain regex in python? 如何在不使用任何库的情况下找出列表是否包含字母? - How can I find out if a list contains letters without using any libraries? 在Python中的另一个列表中查找一个列表中任何项目的最后一次出现 - Find last occurrence of any item of one list in another list in python 如何将列表中的一项转换为 Python 中的字符串? - How do I convert one item of a list into a string in Python? 我如何在python regex中找到单引号内的字符串 - How can i find the string inside single quotes in python regex 如何使用正则表达式使用 Python 按字母顺序查找字符串? - How can I use Regex to find a string of characters in alphabetical order using Python? 如何在Python列表中找到项目的位置? - How can I find the locations of an item in a Python list of lists? 如何在列表python的项目中查找字符串 - How to find string in item of list python 如何在 python 的列表中一遍又一遍地添加一项 - how can I add one item over and over in a list in python Python 的新手,并试图找出如何收集嵌套列表中一个特定项目的数量 - New to Python and trying to find out how to gather the quantity of one specific item in a nested list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM