我有一个字符串,如“嘿人#Greetings我们怎么样?#令人敬畏”,每次有一个hashtag我需要用另一个字符串替换该字。Python正则表达式替换所有匹配
我有下面的代码,当只有一个hashtag工作,但问题是因为它使用sub来替换所有实例,它会覆盖每个字符串与最后一个字符串。
match = re.findall(tagRE, content)
print(match)
for matches in match:
print(matches)
newCode = "The result is: " + matches + " is it correct?"
match = re.sub(tagRE, newCode, content)
我应该怎么做,而不是只取代目前的比赛?有没有使用re.finditer来替换当前匹配或其他方式的方法?
你可以提供一个功能're.sub'做到这一点https://docs.python.org/2/library/re.html #re.sub –
您的预期成果是什么? – manvi77