【发布时间】:2020-12-28 20:25:26
【问题描述】:
Python (3.8.6) 模块 re 在字符串中找到给定的模式,但无法替换它。
# example text
x = """hello world
new line"""
re.search(r"^hello world$", x, re.MULTILINE) -> <re.Match object; span=(0, 11), match='hello world'>
# pattern found
re.sub(r"^hello world$", "SUB", x, re.MULTILINE) -> "hello world\nnew line"
# does not get replaced
谁能解释这种行为?
【问题讨论】:
标签: python python-3.x regex python-re