我有一個字符串,如「嘿人#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