import re
content = "This is a [story] to be told"
tag = r'[story]'
print(re.compile(rf'{tag}').search(content))
this returns: <re.Match object; span=(3, 4), match='s'>
I am using the 'r' prefix in the variable and the 'rf' prefix in the regex. Where am I going wrong? It keeps interpreting my variable as a coded string.
In my actual scenario I have a list of tags: [r'[TAG1]', r'[TAG2]', r'[TAG3]']
which I want to iterate over. I have solved the problem by escaping with '\' in every tag's string assignment but this makes the tags unusable when referencing them later, as 1 of the '\'s is not in the beginning or end of string (since strings are immutable).
I could try using slices but that seems incredibly inefficient. Is there a regex solution?