Last active
June 14, 2020 16:13
-
-
Save kurzweil777/094dd1914244ac713cfa217878b9c1c9 to your computer and use it in GitHub Desktop.
Exercise from code_wars
This file contains hidden or 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 domain_name(url): | |
| """Write a function that when given a URL as a string, parses out just the domain name and returns it as a | |
| string. """ | |
| # Creating a Regular expression to search a website | |
| regex = re.compile(r"""(http://www.|https://www.|www.|http://|https://)?(.*)\.""") | |
| # Writing down matches into result variable and splitting them into groups | |
| result = regex.match(url).group(2).split(".") | |
| # If result contains more than 1 group - remove other groups, else create a string with the name in result | |
| return ''.join([result[0] if len(result) > 1 else ''.join(result)]) | |
| print(domain_name("http://github.com/carbonfive/raygun")) # "github" | |
| print(domain_name("http://www.zombie-bites.com")) # "zombie-bites" | |
| print(domain_name("https://www.cnet.com")) # "cnet" | |
| print(domain_name("http://google.co.jp")) # "google" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment