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 http://www.geeksforgeeks.org/' Output : URLs : ['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'http://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.
# 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 http://www.geeksforgeeks.org/'print("Urls: ", Find(string)) |
Output:
Urls: ['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'http://www.geeksforgeeks.org/']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Check if an URL is valid or not using Regular Expression
- Python | URL shortener using tinyurl API
- Python program to extract Email-id from URL text file
- Python | Extract URL from HTML using lxml
- Django URL patterns | Python
- Python | Sorting URL on basis of Top Level Domain
- Python | Key-Value to URL Parameter Conversion
- response.url - Python requests
- Python | Split URL from Query Parameters
- Python IMDbPY – Getting cover URL of the series
- Python Tweepy – Getting the URL of a user
- Parsing and Processing URL using Python - Regex
- Build an Application to extract URL and Metadata from a PDF using Python
- Python program to convert URL Parameters to Dictionary items
- URL Shorteners and its API in Python | Set-2
- URL Shorteners and its API in Python | Set-1
- url - Django Template Tag
- URL fields in serializers - Django REST Framework
- Pafy - Getting URL of Stream
- Pafy - Getting https URL of Stream
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rajasekharreddydonthireddy

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
