gpt4 book ai didi

python - 从字符串中删除具有特定约束的单词

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:26 25 4
gpt4 key购买 nike

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

最佳答案

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

您可以简单地使用它。查看演示。

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

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

关于python - 从字符串中删除具有特定约束的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28825216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com