Reputation: 8510
Is there an efficient and general way to match regular expressions as well as compositions based on them ?
For instance, if I want matches for each of those 3 strings: "I like"
, "cats"
, and "I like .* cats"
, I can obviously make 3 separate queries.
Is there a more efficient way to do this (preferably in python) ?
Upvotes: 1
Views: 50
Reputation: 2525
patterns = ["I like", "cats", "I like .* cats"]
for stuff in patterns:
re.search(r'%s' % stuff, string_to_search, flags)
More efficient, stops at the first match ordering patterns by lenght:
for stuff in sorted(patterns,key = len):
if re.search(r'%s' % stuff, string_to_search):
break
Upvotes: 1