Python | Check for URL in a String
Prerequisite: Pattern matching with Regular Expression In this article, we will need to accept a string and we need to check if the string contains any URL in it. If the URL is present in the string, we will say URL’s been found or not and print the respective URL present in the string. We will use the concept of Regular Expression of Python to solve the problem.
Examples:
Input : string = 'My Profile: https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles in the portal of https://www.geeksforgeeks.org/' Output : URLs : ['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/'] Input : string = 'I am a blogger at https://geeksforgeeks.org' Output : URL : ['https://geeksforgeeks.org']
To find the URLs in a given string we have used the findall() function from the regular expression module of Python. This return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left to right, and matches are returned in the order found.
Python3
# Python code to find the URL from an input string # Using the regular expression import re def Find(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] # Driver Code string = 'My Profile: https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles in the portal of https://www.geeksforgeeks.org/' print ( "Urls: " , Find(string)) |
Urls: ['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']
Method #2: Using startswith() method
Python3
# Python code to find the URL from an input string def Find(string): x = string.split() res = [] for i in x: if i.startswith( "https:" ) or i.startswith( "http:" ): res.append(i) return res # Driver Code string = 'My Profile: https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles in the portal of https://www.geeksforgeeks.org/' print ( "Urls: " , Find(string)) |
Urls: ['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']
Method #3 : Using find() method
Python3
# Python code to find the URL from an input string def Find(string): x = string.split() res = [] for i in x: if i.find( "https:" ) = = 0 or i.find( "http:" ) = = 0 : res.append(i) return res # Driver Code string = 'My Profile: https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles in the portal of https://www.geeksforgeeks.org/' print ( "Urls: " , Find(string)) |
Urls: ['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']
Time Complexity : O(N)
Auxiliary Space : O(N)
Please Login to comment...