从字符串中删除具有某些约束的单词

时间:2015-03-03 06:10:50

标签: python regex

我在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 。有帮助吗?感谢。

1 个答案:

答案 0 :(得分:2)

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

你可以简单地使用它。参见演示。

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

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