Easy Tutorial
❮ Python Func Number Exp Python Interpreter ❯

Using Regular Expressions to Extract URLs from Strings in Python

Python3 Examples

Given a string that contains URLs, we need to use regular expressions to extract the URLs from the string.

Example

import re

def Find(string):
    # findall() finds all substrings that match the regex
    url = re.findall('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', string)
    return url

string = 'The webpage address for tutorialpro is: https://www.tutorialpro.org, and the webpage address for Google is: https://www.google.com'
print("Urls: ", Find(string))

?: Explanation:

(?:x)

Matches x but does not remember the match. These parentheses are called non-capturing parentheses, allowing you to define subexpressions that can be used with regex operators. Consider this example /(?:foo){1,2}/. If the expression was /foo{1,2}/, {1,2} would only apply to the last character 'o' of 'foo'. With non-capturing parentheses, {1,2} applies to the entire word 'foo'.

Executing the above code gives the following result:

Urls:  ['https://www.tutorialpro.org', 'https://www.google.com']

Python3 Examples

❮ Python Func Number Exp Python Interpreter ❯