Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
Last active June 14, 2020 16:13
Show Gist options
  • Save kurzweil777/094dd1914244ac713cfa217878b9c1c9 to your computer and use it in GitHub Desktop.
Save kurzweil777/094dd1914244ac713cfa217878b9c1c9 to your computer and use it in GitHub Desktop.
Exercise from code_wars
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