Reputation: 21198
I want to check whether a string (a tweet) begins with a '#' (i.e. is a hashtag) or not, and if so create a link.
Below is what I've tried so far but it doesn't work (error on the last line). How can I fix this and will the code work for the purpose?
tag_regex = re.compile(r"""
[\b#\w\w+] # hashtag found!""", re.VERBOSE)
message = raw_message
for tag in tag_regex.findall(raw_message):
message = message.replace(url, '<a href="http://statigr.am/tag/' + message[1:] + '/">' + message + '</a>')
Upvotes: 0
Views: 405
Reputation: 4348
>>> msg = '#my_tag the rest of my tweet'
>>> re.sub('^#(\w+) (.*)', r'<a href="http://statigr.am/tag/\1">\2</a>', msg)
'<a href="http://statigr.am/tag/my_tag">the rest of my tweet</a>'
>>>
Upvotes: 3