我這種情況,我有錯點的句子來處理,這句話(。):正則表達式替換爲字符串的Python
sentence = 'Hi. Long time no see .how are you ?can you follow [email protected]?'
我試圖規範這句話,如果你看到它,有是一些錯誤的格式句子(.how,?can和。@ abcde)。我正在考慮使用正則表達式來處理這個問題,因爲句子不斷變化。這是到目前爲止我的代碼:(。)(?) 進口再
character = ['.','?','@']
sentence = 'Hi. Long time no see .how are you ?can you follow [email protected]?'
sentence = str(sentence)
for i in character:
charac = str(i)
charac_after = re.findall(r'\\'+charac+r'\S*', sentence)
if charac_after:
print("Exist")
sentence = sentence.replace(charac, charac+' ')
print(sentence)
結果一些如何跳過點,並在(@),它只是處理問號。這是結果: 存在
Hi. Long time no see .how are you ? can you follow [email protected]?
它應該是「嗨,好久不見你怎麼可以你關注@ ABCDE。?。?」。我不知道「r'\'+ charac + r'\ S *'」中的雙反斜槓是錯的還是什麼,我錯過了什麼?
如何處理所有角色?請幫忙。
我不知道蟒蛇,但你可能需要正確逃生看到http://stackoverflow.com/questions/280435/escaping-regex-string-in-python – Fallenhero
如果'r'\\''應該轉義下一個字符,則只需要使用單個反斜槓。目前你正在逃避反斜槓。但是,您應該使用're.escape'來代替。 –
但我認爲你的代碼有問題。你會在每一個'。|?| @'之後增加一個空格,這個 – Fallenhero