我需要編寫一個正則表達式來代替'.'
與','
在某些患者對藥物的評論中。他們在提到副作用後應該使用逗號,但其中一些使用了點。例如:正則表達式用客戶意見中的逗號替換一些點
text = "the drug side-effects are: night mare. nausea. night sweat. bad dream. dizziness. severe headache. I suffered. she suffered. she told I should change it."
我寫一個正則表達式的代碼來檢測一個字(如,頭痛)或兩個單詞(如,壞的夢)由兩個點包圍:
檢測由包圍的字兩個點:
text= re.sub (r'(\.)(\s*\w+\s*\.)',r',\2 ', text)
檢測兩個詞用兩個點所包圍:
text = re.sub (r'(\.)(\s*\w+\s\w+\s*\.)',r',\2 ', text11)
這是輸出:
the drug side-effects are: night mare, nausea, night sweat. bad dream, dizziness, severe headache. I suffered, she suffered. she told I should change it.
但它應該是:night sweat to ','
後
the drug side-effects are: night mare, nausea, night sweat, bad dream, dizziness, severe headache. I suffered. she suffered. she told I should change it.
我的代碼並沒有取代dot
。另外,if a sentence starts with a subject pronoun (such as I and she) I do not want to change dot to comma after it, even if it has two words (such as, I suffered)
。我不知道如何將這個條件添加到我的代碼中。
有什麼建議嗎?謝謝 !
請參閱https://regex101.com/r/awW1Hc/1,這是你想達到什麼目的?你將不得不硬編碼代詞,沒有辦法。 –
@ Sebastian Proske,謝謝!完美的作品! – Mary