Created
February 22, 2022 10:19
-
-
Save ibrahimsha23/8e99219e32152cccc03415614d78886d to your computer and use it in GitHub Desktop.
Regex pattern to split the hostname from url path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def url_path_to_dict(path): | |
pattern = (r'^' | |
r'((?P<schema>.+?)://)?' | |
r'((?P<user>.+?)(:(?P<password>.*?))?@)?' | |
r'(?P<host>.*?)' | |
r'(:(?P<port>\d+?))?' | |
r'(?P<path>/.*?)?' | |
r'(?P<query>[?].*?)?' | |
r'$' | |
) | |
regex = re.compile(pattern) | |
m = regex.match(path) | |
d = m.groupdict() if m is not None else None | |
return d | |
def main(): | |
return (url_path_to_dict( | |
'http://ibrahim:password@example.example.com/example/example/example.html')) | |
data = main() | |
print(data) | |
# output: | |
{ | |
'schema': 'http', | |
'user': 'ibrahim', | |
'password': 'password', | |
'host': 'example.example.com', | |
'port': None, | |
'path': '/example/example/example.html', | |
'query': None | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment