我不是 Python 新手,而是一个使用正则表达式的完整新手(在我的待办事项列表中)
我正在尝试使用 python re 来转换字符串,例如
[Hollywood Holt](http://www.hollywoodholt.com)
到
<a href="http://www.hollywoodholt.com">Hollywood Holt</a>
和一个字符串
*Hello world*
到
<strong>Hello world</strong>
你为什么要费心使用正则表达式?您的内容是 Markdown,为什么不简单地获取字符串并通过 markdown 模块运行它?
首先,确保已安装 Markdown。它对 ElementTree 有依赖关系,所以如下所示轻松安装它们两个。如果您运行的是 Windows,则可以改用Windows 安装程序。
easy_install ElementTree
easy_install Markdown
要使用 Markdown 模块并将字符串转换为 html,只需执行以下操作(三引号用于文字字符串):
import markdown
markdown_text = """[Hollywood Holt](http://www.hollywoodholt.com)"""
html = markdown.markdown(markdown_text)