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)
Do this:
result = re.sub(r"http\S+", "", subject)
http
matches literal characters\S+
matches all non-whitespace characters (the end of the url)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
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 occurrencesYou 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:'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With