About Handling Twitter Usernames

You can convert twitter handles to links with the following function:

1
2
3
4
5
6
7
def convert_twitter_usernames_to_links(html):
    import re
    pattern = re.compile(r"(^|[^@\w])@(\w{1,15})\b")
    return pattern.sub(
        r'\1<a href="https://twitter.com/\2">@\2</a>',
        html,
    )

Tips and Tricks Programming User Experience Python 3