我想清理字符串,使其沒有任何標點符號或數字,它必須只有a-z和A-Z。 例如,給定字符串是:正則表達式跳過一些特定字符
"coMPuter scien_tist-s are,,, the rock__stars of tomorrow_ <cool> ????"
需要的輸出是:
['computer', 'scientists', 'are', 'the', 'rockstars', 'of', 'tomorrow']
我的解決辦法是
re.findall(r"([A-Za-z]+)" ,string)
我的輸出
['coMPuter', 'scien', 'tist', 's', 'are', 'the', 'rock', 'stars', 'of', 'tomorrow', 'cool']
你最好的解決辦法是o使用一個簡單的替換刪除所有字符不是az和空間。 ''[^ A-Za-z] +'(你可以用'\ s'代替右括號後面的空格,然後用空格作爲分隔符對字符串進行分割。在正則表達式中,你可以 –
請詳細說明一下,請問? –
@cfqueryparam謝謝你我在說什麼re.sub(r'([^ a-zA-Z \ s] +)','',s).split() –