0
我已經寫了一個函數來替換句子中的單詞,而不使用替換方法中的python,問題是我的代碼在邊緣情況下失敗單詞與另一個詞相結合,我想可能會替換每一個可能的事件。看看我的代碼如何在不使用python替換方法的情況下替換句子中的單詞
def replace_all (target,find,replace):
split_target = target.split()
result = ''
for i in split_target:
if i == find:
i = replace
result += i + ' '
return result.strip()
target = "Maybe she's born with it. Maybe it's Maybeline."
find = "Maybe"
replace = "Perhaps"
print replace_all(target, find, replace)
輸出爲:
Perhaps she's born with it. Perhaps it's Maybeline.
,但我希望它打印:
Perhaps she's born with it. Perhaps it's perhapsline.
公告的最後一句話是maybeline是假設改變或許線。我已經與此戰鬥了一週,任何幫助將不勝感激。
是正則表達式公平遊戲嗎? –