感谢,写了一个能用的
def convertMsgToHTML(msg):
#urls = re.findall('http[s]?://(?:#|[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', msg)
msg = re.sub(r'( http[s]?://(?:#|[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)', r'<a href="\g<1>" target="_blank">\g<1></a>',msg,count=0,flags=re.UNICODE|re.MULTILINE)
return msg
测试:
msg = u'''特殊 123
http://g.cn 456
http://g.cn/1234 78910 汉字
'''
text = convertMsgToHTML(msg)
print text.encode('utf-8')
输出结果:
特殊 123 <a href="
http://g.cn" target="_blank">
http://g.cn</a> 456 <a href="
http://g.cn/1234" target="_blank">
http://g.cn/1234</a> 78910 汉字