【发布时间】:2021-09-30 06:41:01
【问题描述】:
import re
text="Her sweet-natured father is constantly henpecked by his domineering wives, who rule their domains with iron fists. His wives were named Alexis, Eris, Irer, Zenith and Saunder."
text=re.sub(r'r$\b','rh',text)
print(text)
期望的输出:
她和蔼可亲的父亲总是被他的霸道吓到 妻子,用铁腕统治他们的领域。他的妻子被命名为 Alexis、Eris、Irerh、Zenith 和 Saunderh。
输出:
她和蔼可亲的父亲总是被他的霸道吓到 妻子,他们用铁腕统治他们的领域。他的妻子被命名为 Alexis、Eris、Irer、Zenith 和 Saunder。
即,字符串中没有发生任何变化。有什么问题吗?
【问题讨论】:
-
为什么
"Her"在您想要的输出中不受影响? -
测试你的正则表达式,例如regex101.com,从描述上应该问题很明显了。
-
这里也不适合使用正则表达式。这是家庭作业吗?
-
$在正则表达式中做了什么? -
@ddejohn 这是我正在进行的项目的一部分,而不是家庭作业