【发布时间】:2015-08-05 05:35:14
【问题描述】:
我有以下代码。
def render_markdown(markdown):
"Replaces markdown links with plain text"
# non greedy
# also includes images
RE_ANCHOR = re.compile(r"\[[^\[]*?\]\(.+?\)")
# create a mapping
mapping = load_mapping()
anchors = RE_ANCHOR.findall(markdown)
counter = -1
while len(anchors) != 0:
for link in anchors:
counter += 1
text, href = link.split('](')[:2]
text = '-=-' + text[1:] + '-=-'
text = text.replace(' ', '_') + '_' + str(counter)
href = href[: -1]
mapping[text] = href
markdown = markdown.replace(link, text)
anchors = RE_ANCHOR.findall(markdown)
return markdown, mapping
但是,markdown 函数不会替换所有链接。几乎没有一个被替换。我环顾四周,发现了很多与此有关的问题。发现的问题属于以下类型:
abc.replace(x, y) instead of abc = abc.replace(x, y)
我正在这样做,但字符串没有被替换
【问题讨论】:
-
你能举一个你期望发生的例子吗?
-
你为什么不使用many markdown libraries 来准备安装?
-
@kindall 这是一种自我教育。因此,我正在从头开始构建。我期待search 被替换为 -=-search-=-
标签: python python-2.7 replace markdown