'Combining a list of regex to search a corpus for the union of all of them
I have a list() of compiled regular expressions:
my_list = []
my_list.append(re.compile("^foo")) # starts with 'foo'
...
my_list.append(re.compile("bar$")) # ends with 'bar'
I then want to search for lines that match ALL of the regex in the list.
My list builder builds a variable number of regex.
This approach probably "works", but seems inefficient since the number of regex and the corpus for searching may be large:
miss = False
for pattern in my_list:
if not pattern.search(line):
miss = True
break
if not miss:
print('\'{}\' DID match all patterns'.format(line))
else:
print('\'{}\' did NOT match all patterns'.format(line))
It seems like I should be able to munge all the regexes together and do a single, more efficient, search...
combo = re.munge(my_list) # <-- is there such a 'join?' method ???
if combo.search(line):
print('\'{}\' DID match all patterns'.format(line))
else:
print('\'{}\' did NOT match all patterns'.format(line))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|