A regular expression starts its life as a string, so left_identifier + text + right_identifier and use this in re.compile
Or:
re.findall('{}(.*){}'.format(left_identifier, right_identifier), text)
works too.
You need to avoid strings in variables if they contain the regex metacharacter with re.escape unless you want the metacharacters to be interpreted as such
>>> text = "<b*>Test</b>" >>> left_identifier = "<b*>" >>> right_identifier = "</b>" >>> s='{}(.*?){}'.format(*map(re.escape, (left_identifier, right_identifier))) >>> s '\\<b\\*\\>(.*?)\\<\\/b\\>' >>> re.findall(s, text) ['Test']
Str.partition (var) , on the other hand, is an alternative way to do this:
>>> text.partition(left_identifier)[2].partition(right_identifier)[0] 'Test'
source share