Python 正则表达式搜索和替换

标签 python regex string markdown

我不是 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/

相关文章:

python - 如何列出机器人框架测试套件使用的所有关键字

python - 为什么 numpy.linalg.solve() 提供比 numpy.linalg.inv() 更精确的矩阵求逆?

python - 在 numpy(子)数组中除以零

php - 使用正则表达式匹配具有特定ID的div block

php - 是否可以从 preg_match 中捕获任何错误并显示常见错误消息?

mysql - 在mysql中搜索子字符串

java - 匹配特殊字符前的字符串

python - 将文件传递给 docker 命令

Python:正则表达式在空格上分割(但在[]中保持元素不分割)并在数组中添加 ""作为换行符

php - 检查字符串是否与 php 中的另一个字符串相似?