2015-03-03 68 views
2

我在Python中使用RE刪除文本中的所有符號以及以#,@等開頭的一些詞,但無法刪除以http開頭的單詞。我該怎麼做? 這是我的代碼。刪除字符串中的某些約束詞

text = http://twitpic.com/2y1zl - Awww, that's a bummer. You shoulda got David Carr of Third :)))) 
line = re.sub('([!,".?$&\)\(\/\\,:;-]|@\w+|#\w+|http\w+)', '', text) 

我得到的輸出是:

httptwitpiccom2y1zl Awww that's a bummer You shoulda got David Carr of Third 

我不希望在我的輸出httptwitpiccom2y1zl。任何幫助?謝謝。

回答

2
([!,".?$&\)\(\/\\,:;-]|@\w+|#\w+|http\S+) 

您可以簡單地使用this.See demo。

https://regex101.com/r/wU7sQ0/51

line = re.sub('([!,".?$&\)\(\/\\,:;-]|@\w+|#\w+|http\S+)', '', text) 
+0

你可以使用'string'模塊,而不是硬編碼提高你的答案。 ''['+ string.punctuation +']''。 – Jimilian 2015-03-03 06:18:36

+0

感謝您的網址,這將是有益的。 – User10910251 2015-03-03 06:19:29

+0

@Jimilian我不想讓''「符號包含在列表中。我可以使用'['+ string.punctuation +']',假設它也包含上述符號嗎? – User10910251 2015-03-03 06:21:33