Python program to check for URL in a string


This article will teach you how to determine whether a string contains a URL or not. In Python, strings are collections of bytes that represent Unicode characters. You can use single or double quotes and everything enclosed in them is considered as a string. When given a string, we will first determine whether it contains a URL. If one is found, we will then print the URL.

Using findall() method

We will employ Python's regular expression concept to resolve this issue. Regular expressions are supported by the Python re package. Using a particular syntax defined in a pattern, a regular expression is a special sequence of characters that helps in matching or finding other strings or sets of strings.

Each string in the list returned by the findall() method denotes a different match that was found. By scanning the string from left to right, this method returns matches in the order that they are found.

Algorithm

Following algorithm demonstrates how to check for url in a string using findall() method −

  • re module import

  • Create a function to locate the URL.

  • Create a regular expression in the function that stores each character that can be in a URL.

  • Declare a second variable that will store each string that fits the URL pattern.

  • Print the list's strings all at once.

  • Declare a string with the characters.

  • Print the value returned by the function after passing the string there.

Example

In this program, we have utilised a re module method that will search a provided string for a specified pattern. We must import the re module into the program in order to use the method. The program will display an empty list if the string doesn't contain any URLs.

import re
def checkURL(str):
# findall() function used with the conditions which is valid for url in the strings
# The regex function can store all the characters including the upper case and the lower case of the alphabets, numbers, special cases and characters etc 8. Python program to check for url in a string

   regex= 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+' 
   URL= re.findall(regex,str) 
   return URL 
# The driver code 
m = "https://www.tutorialspoint.com/python-program-to-check-for-url-in-a-string" 
print("The url is: ", checkURL(m))

Output

Following is an output of the above code −

The url is:  ['https://www.tutorialspoint.com/python-program-to-check-for-url-in-a-string']

Example

In the Python code mentioned below, we have created a regular expression for URL to verify the URL in the string, and we are using the built-in method findall() to check the URL pattern in the input string. The result is returned after the findall() function scans the string from left to right −

import re
def checkURL(str):
# findall() function used with the conditions which is valid for url in the strings
   regex= 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
   checkURL= re.findall(regex,str)
   if checkURL:
      return "url in the string is : ",checkURL
   else:
      return "URL is not present"
# The driver code
m = input("Provide the string: ")
print(checkURL(m))

Output

Following are the two cases of output −

Case-1

Following is an output of the above code when the url pattern is not correctly provided −

Provide the string: Providing this like url
The url is: URL is not present

Case-2

Following is an output when the url is provided correctly −

Provide the string: https://www.tutorialspoint.com/python-program-to-check-for-url-in-a-string
('url in the string is : ', ['https://www.tutorialspoint.com/python-program-to-check-for-url-in-a-string'])

Using search() method

A regular expression search in Python is commonly expressed as: match = re.search (path, string). The re.search() method looks for a regular expression pattern within a string using a regular expression pattern and a string. Search() returns a match object or None if the search is successful.

Example

The search() method of the re module, which returns the desired result as a URL, is used in the code given below −

import re
# findall() function used with the conditions which is valid for url in the strings
string = "https://www.tutorialspoint.com/python-program-to-check-for-url-in-a-string"
regex= 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
result = re.search(regex,string).group()
print("The URL is: ", result)

Output

Following is an output of the above code −

The URL is:  https://www.tutorialspoint.com/python-program-to-check-for-url-in-a-string

Updated on: 04-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements