Parsing and Processing URL using Python - Regex - GeeksforGeeks

02 Sep, 2020
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 libraryimport re    # url links = 'https://www.geeksforgeeks.org/'  # finding the protocol obj1 = re.findall('(\w+)://',                  s)print(obj1)  # finding the hostname which may# contain dash or dotsobj2 = 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 libraryimport re    # url links = 'file://localhost:4040/abc_file'  # finding the file capture groupobj1 = re.findall('(\w+)://', s)  print(obj1)  # finding the hostname which may # contain dash or dotsobj2 = re.findall('://([\w\-\.]+)', s)print(obj2)  # finding the hostname which may # contain dash or dots or port# numberobj3 = 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 libraryimport re  # urls = 'http://www.example.com/index.html'   # searching for all capture groupsobj = re.findall('(\w+)://([\w\-\.]+)/(\w+).(\w+)',                 s)  print(obj)
Output:
[('http', 'www.example.com', 'index', 'html')]
python-regex
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | Get unique values from a list
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()