我不是 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,所以 easy_install 它们两个如下。如果您运行的是 Windows,则可以使用 Windows installer相反。
easy_install ElementTree
easy_install Markdown
要使用 Markdown 模块并将字符串转换为 html,只需执行以下操作(三重引号用于文字字符串):
import markdown
markdown_text = """[Hollywood Holt](http://www.hollywoodholt.com)"""
html = markdown.markdown(markdown_text)
关于Python 正则表达式搜索和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/933824/