我有一组字符串,我想询问它所有包含的元素,这些元素与特定的 RegEx 匹配。例如:
set = set(['abcd', 'abdd', 'acdd'])
set.findAll('ab.d')
这应该返回:
['abcd', 'abdd']
有办法吗?
最佳答案
遍历集合中的所有元素,并在正则表达式上进行过滤:
>>> s = {'abcd', 'abdd', 'acdd'}
>>> r = [el for el in s if re.search('ab.d', el)]
>>> r
['abcd', 'abdd']
关于python - 使用正则表达式查询集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29657644/