【发布时间】:2018-04-26 19:41:32
【问题描述】:
我正在尝试编写一个 python 函数并将 markdown 格式的链接(如:[text](链接))转换为 HTML 中的 标记。例如:
链接(行)
link("Here is the link [Social Science Illustrated](https://en.wikipedia.org/wiki/Social_Science_Illustrated) I gave you yesterday.")
"Here is the link <a href="https://en.wikipedia.org/wiki/Social_Science_Illustrated">Social Science Illustrated</a> I gave you yesterday."
我现在的代码是:
def link(line):
import re
urls = re.compile(r"((https?):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)")
line = urls.sub(r'<a href="\1"></a>', line)
return line
输出:
=> 'Here is the link [Social Science Illustrated] ( <a href="https://en.wikipedia.org/wiki/Social_Science_Illustrated"></a> ) I gave you yesterday.'
所以我想知道如何将 [text] 部分转换为正确的位置?
【问题讨论】: