I have a string with a list of usernames mentioned for instance:
s = '@romeo went to @juliet and said hi, I'm @romeo'
I want to replace that username patter with links to user profile which should become <a href="/u/username">@username</a>
I am now able to replace the patterns, however, I cannot seem to get rid of the @ in the href with using backreferences.
print(re.sub(r"(^|[^@\w])@(\w{1,31})", r'<a href="/u/\g<0>">\g<0></a>', s))
That right now prints:
<a href="/u/@romeo">@romeo</a> went to<a href="/u/ @juliet"> @juliet</a> and said hi, Im<a href="/u/ @romeo"> @romeo</a>
Which now you can see the extra space and @ I cannot seem to get rid of that after using the regex