find urls in a string in python

To find URLs in a string in Python, we can use regular expressions (regex).

Here is an example code block that shows how to use regex to find URLs in a string:

main.py
import re

string_with_urls = "This is a string with a URL: https://www.example.com and another URL https://www.anotherexample.com/somepath"

urls = re.findall("(?P<url>https?://[^\s]+)", string_with_urls)

print(urls) 
220 chars
8 lines

The output of the code will be a list of all the URLs found in the string:

main.py
['https://www.example.com', 'https://www.anotherexample.com/somepath']
71 chars
2 lines

In the regex pattern, https?:// matches both http:// and https://, [^\s]+ matches any characters that are not whitespace until the end of the URL is reached. The re.findall() function returns all non-overlapping matches of the regex pattern in the input string.

gistlibby LogSnag