Python 查找以'开头的字符串#';并创建链接

Python 查找以'开头的字符串#';并创建链接,python,regex,django,string,hashtag,Python,Regex,Django,String,Hashtag,我想检查字符串(tweet)是否以“#”开头(即是一个hashtag),如果是,则创建一个链接 下面是我到目前为止尝试过的,但不起作用(最后一行出现错误)。 我如何解决这个问题,代码是否会为此而工作 tag_regex = re.compile(r""" [\b#\w\w+] # hashtag found!""", re.VERBOSE) message = raw_message for tag in tag_regex.findall(raw_message

我想检查字符串(tweet)是否以“#”开头(即是一个hashtag),如果是,则创建一个链接

下面是我到目前为止尝试过的,但不起作用(最后一行出现错误)。 我如何解决这个问题,代码是否会为此而工作

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>')
tag_regex=re.compile(r”“”
[\b#\w\w+]#找到了标签!“”,re.VERBOSE)
消息=原始消息
对于tag_regex.findall(原始消息)中的标记:
message=message.replace(url“”)
>>msg='#我的_标记我的tweet的其余部分'
>>>re.sub('^#(\w+)(.*),r'',msg)
''
>>> 
>>msg='#我的_标记我的tweet的其余部分'
>>>re.sub('^#(\w+)(.*),r'',msg)
''
>>> 

您收到了什么错误?@Haidro:“分配前引用了局部变量‘url’”您的代码中必须有更多内容,然后您没有向我们显示您收到了什么错误?@Haidro:“分配前引用了局部变量‘url’”您的代码中必须有更多内容,然后您没有向我们显示
>>> 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>'
>>>