For clarity, i was looking for a way to compile multiple regex at once.
For simplicity, let's say that every expression should be in the format (.*) something (.*).
There will be no more than 60 expressions to be tested.
As seen here, i finally wrote the following.
import re
re1 = r'(.*) is not (.*)'
re2 = r'(.*) is the same size as (.*)'
re3 = r'(.*) is a word, not (.*)'
re4 = r'(.*) is world know, not (.*)'
sentences = ["foo2 is a word, not bar2"]
for sentence in sentences:
match = re.compile("(%s|%s|%s|%s)" % (re1, re2, re3, re4)).search(sentence)
if match is not None:
print(match.group(1))
print(match.group(2))
print(match.group(3))
As regex are separated by a pipe, i thought that it will be automatically exited once a rule has been matched.
Executing the code, i have
foo2 is a word, not bar2
None
None
But by inverting re3 and re1 in re.compile match = re.compile("(%s|%s|%s|%s)" % (re3, re2, re1, re4)).search(sentence), i have
foo2 is a word, not bar2
foo2
bar2
As far as i can understand, first rule is executed but not the others. Can someone please point me on the right direction on this case ?
Kind regards,