regex - How to match exact "multiple" strings in Python -
i've got list of exact patterns want search in given string. i've got real bad solution such problem.
pat1 = re.compile('foo.tralingstring') mat1 = pat1.match(mystring) pat2 = re.compile('bar.trailingstring') mat2 = pat2.match(mystring) if mat1 or mat2: # whatever pat = re.compile('[foo|bar].tralingstring') match = pat.match(mystring) # doesn't work
the condition i've got list of strings matched exactly. whats best possible solution in python.
edit: search patterns have trailing patterns common.
you trivial regex combines two:
pat = re.compile('foo|bar') if pat.match(mystring): # whatever
you expand regex whatever need to, using |
separator (which means or in regex syntax)
edit: based upon recent edit, should you:
pat = re.compile('(foo|bar)\\.trailingstring'); if pat.match(mystring): # whatever
the []
character class. [foo|bar]
match string one of included characters (since there's no * or + or ? after class). ()
enclosure sub-pattern.
Comments
Post a Comment