Lets say I have a list of strings,
string_lst = ['fun', 'dum', 'sun', 'gum']
I want to make a regular expression, where at a point in it, I can match any of the strings i have in that list, within a group, such as this:
import re
template = re.compile(r".*(elem for elem in string_lst).*")
template.match("I love to have fun.")
What would be the correct way to do this? Or would one have to make multiple regular expressions and match them all separately to the string?
Except for the regular expression, you can use list comprehension, hope it's not off the topic.
You cannot use
match
as it will match from start.Usefindall
instead.Output:
['fun']
using
search
you will get only the first match.So usefindall
instead.Also use
lookahead
if you have overlapping matches not starting at the same point.In line with @vks reply - I feel this actually does the comeplete task..
Adding word boundary completes the task!
regex
module has named lists (sets actually):Here
words
is just a name, you can use anything you like instead..search()
methods is used instead of.*
before/after the named list.To emulate named lists using stdlib's
re
module:re.escape()
is used to escape regex meta-characters such as.*?
inside individual words (to match the words literally).sorted()
emulatesregex
behavior and it puts the longest words first among the alternatives, compare:You should make sure to escape the strings correctly before combining into a regex