【发布时间】:2014-07-28 11:12:15
【问题描述】:
以下代码:
>>> text = "imagine a new *world*, a *magic* world"
>>> pattern = re.compile(r'\*(.*?)\*')
>>> pattern.sub(r"<b>\1<\b>", text)
输出:
imagine a new <b>world<\x08>, a <\b>magic<\x08> world
我这里有两个问题,
1.) 我不明白为什么反向引用 '\1' 会更改文本的 magic 部分?
我读到'\1' 指的是被捕获的第一组。
2.) 为什么 <\b> 输出 <\x08> 即使在使用 'r' 作为前缀之后。 '\n' 不会发生这种情况。
【问题讨论】:
-
@Bakuriu 是的,但是它应该输出:想象一个新的 world,一个 world 世界,因为 \1 指的是“世界”跨度>
标签: python regex python-2.7 python-3.x regex-group