查找字符串的一部分并替换

时间:2019-12-29 20:42:36

标签: python regex

我希望能够找到字符串的一部分,但在python3中替换整个字符串。

例如,找到所有低期望*(例如最低期望,低期望,低期望)的实例,然后将其替换为“低期望”。

我尝试使用:

x = re.sub("low$ expectation$", 'low expectation',"I have low expectations about the future.")

1 个答案:

答案 0 :(得分:1)

使用\w来匹配单词字符:

x = re.sub(r"low\w* expectation\w*", 'low expectation',"I have low expectations about the future.")
相关问题