用于从Twitter推文中删除URL链接的表达式

hag*_*ope 13 python regex string

我只是想在字符串中找到并替换所有出现的twitter url(tweet):

输入:

这是一条带有网址的推文:http://t.co/0DlGChTBIx

输出:

这是一条带有网址的推文:

我试过这个:

p=re.compile(r'\<http.+?\>', re.DOTALL)
tweet_clean = re.sub(p, '', tweet)
Run Code Online (Sandbox Code Playgroud)

zx8*_*x81 45

做这个:

result = re.sub(r"http\S+", "", subject)
Run Code Online (Sandbox Code Playgroud)
  • http 匹配文字字符
  • \S+ 匹配所有非空白字符(网址的末尾)
  • 我们用空字符串替换