I'm creating a django filter for inserting 'a' tags into a given string from a list.
This is what I have so far:
def tag_me(text):
tags = ['abc', 'def', ...]
tag_join = "|".join(tags)
regex = re.compile(r'(?=(.))(?:'+ tag_join + ')', flags=re.IGNORECASE)
return regex.sub(r'<a href="/tag/\1/">\1</a>', text)
Example:
tag_me('some text def')
Returns:
'some text <a href="/tag/d/">d</a>'
Expected:
'some text <a href="/tag/def/">def</a>'
The issue lies in the regex.sub as it matches but returns only the first character. Is there a problem with the way I'm capturing/using \1 on the last line ?