Skip to content
Advertisement

Regular expression for removing all URLs in a string in Python

I want to delete all the URLs in the sentence.

Here is my code:

JavaScript

But a URL with “http” is still left in the sentence.

JavaScript

How can I fix it?

Advertisement

Answer

One simple fix would be to just replace the pattern https?://S+ with an empty string:

JavaScript

This prints:

JavaScript

My pattern assumes that whatever non whitespace characters which follow http:// or https:// are part of the URL.

Advertisement