Python Get URL From String Example

Published On: 21/06/2022 | Category: Python

Hey Friends,

This is a short guide on regex to extract url from string python. it's simple example of extract url from string python. you can see python extract url from string. We will look at example of python regex find all urls in string.

The findall()function is used to find all instances matching with the regular expression.

Here i will give you simple example of how to extract url from string in python example, So let's see the below example:

import re
def getUrl(string):
    # findall() has been used 
    # with valid conditions for urls in string
    regex = r"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))"
    url = re.findall(regex,string)      
    return [x[0] for x in url]

string = 'Python Program List:'

I Hope It will help you....