Prerequisite: Regular Expression in Python
URL or Uniform Resource Locator consists of many information parts, such as the domain name, path, port number etc. Any URL can be processed and parsed using Regular Expression. So for using Regular Expression we have to use re library in Python.
Example:
URL: https://www.geeksforgeeks.org/courses When we parse the above URL then we can find Hostname: geeksforgeeks.com Protocol: https
We are using re.findall( ) function of re library for searching the required pattern in the URL.
Syntax: re.findall(regex, string)
Return: all non-overlapping matches of pattern in string, as a list of strings.
Now, let’s see the examples:
Example 1: In this Example, we will be extracting the protocol and the hostname from the given URL.
- Regular expression for extracting protocol group: ‘(\w+)://‘.
- Regular expression for extracting hostname group: ‘://www.([\w\-\.]+)‘.
Meta characters Used:
- \w: Matches any alphanumeric character, this is equivalent to the class [a-zA-Z0-9_].
- +: One or more occurrences of previous characters.
Code:
Python3
# import library import re # url link # finding the protocol obj1 = re.findall('(\w+)://', s) print(obj1) # finding the hostname which may # contain dash or dots obj2 = re.findall('://www.([\w\-\.]+)', s) print(obj2) |
Output:
['https'] ['geeksforgeeks.org']
Example 2: If the URL is of a different type such as ‘file://localhost:4040/zip_file‘, with the port number along with it, then to extract the port number, as it is optional we will use the ‘?’ notation. Here the port number ‘4040′ occurs after the ‘:’ sign. Therefore, as it is a digit (:(\d+)) is used. To make it optional as all URLs do not end with host number, this syntax is used ‘(:(\d+))?’.
Meta characters Used:
- \d: Matches any decimal digit, this is equivalent to the set class [0-9].
- +: One or more occurrences of previous characters.
- ?: Matches zero or one occurrence.
Code:
Python3
# import library import re # url link # finding the file capture group obj1 = re.findall('(\w+)://', s) print(obj1) # finding the hostname which may # contain dash or dots obj2 = re.findall('://([\w\-\.]+)', s) print(obj2) # finding the hostname which may # contain dash or dots or port # number obj3 = re.findall('://([\w\-\.]+)(:(\d+))?', s) print(obj3) |
Output:
['file']
['localhost']
[('localhost', ':4040', '4040')]
Example 3: For a general URL, this can be used, where the path elements can also be constructed.
Python3
# import library import re # url # searching for all capture groups obj = re.findall('(\w+)://([\w\-\.]+)/(\w+).(\w+)', s) print(obj) |
Output:
[('http', 'www.example.com', 'index', 'html')]
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:
- NLP | Partial parsing with Regex
- Point Processing in Image Processing using Python-OpenCV
- Command-Line Option and Argument Parsing using argparse in Python
- XML parsing in Python
- Parsing XML with DOM APIs in Python
- Argparse VS Docopt VS Click - Comparing Python Command-Line Parsing Libraries
- Parsing PDFs in Python with Tika
- Python | Swap Name and Date using Group Capturing in Regex
- Python program to Count Uppercase, Lowercase, special character and numeric values using Regex
- Parsing tables and XML with BeautifulSoup
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- Python | Program that matches a word containing 'g' followed by one or more e's using regex
- The most occurring number in a string using Regex in python
- Name validation using IGNORECASE in Python Regex
- Python - Substituting patterns in text using regex
- Categorize Password as Strong or Weak using Regex in Python
- How to check if a string starts with a substring using regex in Python?
- Python program to find files having a particular extension using RegEx
- Python - Check if String Contain Only Defined Characters using Regex
- Converting a 10 digit phone number to US format using Regex in Python
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.

