Python program to check for URL in a string

PythonProgrammingServer Side Programming

In this case we use re module in Python, Here we accept a string and check if the string contains ant URL in it. If the URL is present in the string then display. We use findall () method to solve this problem.


Step 1: given string as input.
Step 2: findall() function is return all non-overlapping matches of pattern in string and in this function the string is scanned left to right and matches are returned in the order found.

Example Code

# Program to find the URL from an input string
import re
def url(str):
   # findall() has been used
   # with valid conditions for urls in string
   ur = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str)
   return ur
   # Driver Code
   str = ' / user / python program /'
print("Url is :: ", url(str))


Url is :: [' / user / python program /']
Updated on 24-Jun-2020 07:27:30