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.

Algorithm

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 = 'https://auth.mywebsite.org / user / python program / http://www.mywebsite.org/'
print("Url is :: ", url(str))

Output

Url is :: ['https://auth.mywebsite.org / user / python program / http://www.mywebsite.org/']
raja
Published on 26-Sep-2018 13:10:31
Advertisements