正则表达式替换包含指定子字符串的单词

问题描述

我正在尝试替换字符串中包含某个子字符串的单词。这是一个例子

import regex as re

given_in = 'My cat is not like other cats'
desired_out = 'My foo is not like other foo'

我试过了

print(re.sub('cat','foo',given_in))
>>>> 'My foo is not like other foos'

print(re.sub('.*cat.*',given_in))
>>>> 'foo'

这里的正确方法是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)