ADVERTISEMENTS

How to find url from string in python

Strings are immutable, meaning they cannot be changed once they are created. you can use regular expressions to find URLs in a string. The re module provides a variety of functions for working with regular expressions. This regular expression (https?://[^\s]+) will match any string that starts with "http://" or "https://" and continues until the next space character.

The following example will explain about the way to find url from string.

import re

string = "Here is a link to google.com and another one to example.com"

urls = re.findall(r'(https?://[^\s]+)', string)

print(urls)

 

Python is a high-level, interpreted programming language that is widely used for web development, scientific computing, data analysis, artificial intelligence, and more. It is known for its easy-to-read syntax and ability to handle a wide variety of programming tasks with minimal code. You can learn python from our Python Tutorials and Python Examples

ADVERTISEMENTS