Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove any URL within a string in Python

I want to remove all URLs inside a string (replace them with "") I searched around but couldn't really find what I want.

Example:

text1 text2 http://url.com/bla1/blah1/ text3 text4 http://url.com/bla2/blah2/ text5 text6 http://url.com/bla3/blah3/ 

I want the result to be:

text1 text2 text3 text4 text5 text6 
like image 895
Taha Avatar asked Jul 04 '12 15:07

Taha


People also ask

How do I remove a URL from a string in Python?

Use the re. sub() method to remove URLs from text, e.g. result = re. sub(r'http\S+', '', my_string) .

How do you remove URL from text?

To remove a hyperlink but keep the text, right-click the hyperlink and click Remove Hyperlink. To remove the hyperlink completely, select it and then press Delete.

How do I remove a link in Python?

The re. sub() function provides the most straightforward approach to remove URLs from text in Python. This function is used to substitute a given substring with another substring in any provided string. It uses a regex pattern to find the substring and then replace it with the provided substring.

How do I remove 20 from a URL in Python?

replace('%20+', '') will replace '%20+' with empty string.


1 Answers

the shortest way

re.sub(r'http\S+', '', stringliteral) 
like image 64
tolgayilmaz Avatar answered Oct 10 '22 13:10

tolgayilmaz