Markdown 强调 - 替换正则表达式
你会怎么用正则表达式来实现Markdown中的强调
或加粗
呢?
或者说,怎么能把正则表达式\*\*(.*)\*\*
替换成** **
里面的内容呢?
2 个回答
2
我觉得 Fantasizer 的想法很不错。
另外,你还应该看看这个基于Python的 Markdown库。
特别是可以查看一下 inlinepatterns.py
文件,看看它是怎么处理 'strong'(加粗)和 'emphasis'(强调)的。
3
你可以使用 re.sub()
这个函数:
import re
myRegex = re.compile(r"\*\*(.+?)\*\*")
string = "some **text** and some **more**"
output = myRegex.sub(r"\1", string)