Expression to remove URL links from Twitter tweet

Question:

I simply would like to find and replace all occurrences of a twitter url in a string (tweet):

Input:

This is a tweet with a url: http://t.co/0DlGChTBIx

Output:

This is a tweet with a url:

I’ve tried this:

p=re.compile(r'<http.+?>', re.DOTALL)
tweet_clean = re.sub(p, '', tweet)
Asked By: hagope

||

Answers:

Do this:

result = re.sub(r"httpS+", "", subject)
  • http matches literal characters
  • S+ matches all non-whitespace characters (the end of the url)
  • we replace with the empty string
Answered By: zx81

The following regex will capture two matched groups: the first includes everything in the tweet until the url and the second will catch everything that will come after the URL (empty in the example you posted above):

import re
str = 'This is a tweet with a url: http://t.co/0DlGChTBIx'
clean_tweet = re.match('(.*?)http.*?s?(.*?)', str)
if clean_tweet: 
    print clean_tweet.group(1)
    print clean_tweet.group(2) # will print everything after the URL 
Answered By: Nir Alfasi

You could try the below re.sub function to remove URL link from your string,

>>> str = 'This is a tweet with a url: http://t.co/0DlGChTBIx'
>>> m = re.sub(r':.*$', ":", str)
>>> m
'This is a tweet with a url:'

It removes everything after first : symbol and : in the replacement string would add : at the last.

This would prints all the characters which are just before to the : symbol,

>>> m = re.search(r'^.*?:', str).group()
>>> m
'This is a tweet with a url:'
Answered By: Avinash Raj

Try using this:

text = re.sub(r"httpS+", "", text)
Answered By: Garima Rawat

clean_tweet = re.match(‘(.*?)http(.*?)s(.*)’, content)

while (clean_tweet):
content = clean_tweet.group(1) + ” ” + clean_tweet.group(3)
clean_tweet = re.match(‘(.*?)http(.*?)s(.*)’, content)

Answered By: nancy agarwal
text = re.sub(r"https:(//t.co/([A-Za-z0-9]|[A-Za-z]){10})", "", text)

This matches alphanumerics too after t.co/

Answered By: self.Fool

you can use:

text = 'Amazing save #FACup #zeebox https://stackoverflow.com/tiUya56M Ok'
text = re.sub(r'https?://S*', '', text, flags=re.MULTILINE)

# output: 'Amazing save #FACup #zeebox  Ok'
  • r The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with ‘r’
  • ? Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. https? will match either ‘http’ or ‘https’.
  • https?:// will match any "http://" and "https://" in string
  • S Returns a match where the string DOES NOT contain a white space character
  • * Zero or more occurrences
Answered By: Mohammad Nazari

I found this solution:

text = re.sub(r'https?://S+|www.S+', '', text)
Answered By: Julia Stanina
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.