Improved regex search by making it (relatively) simpler, also added a new test case
Regex Explanation:
let's break the regex bit by bit
- (?:\w+.|//|)
- (?:) means a "non capturing group", so the findall function will not return what it finds inside this group.
- \w+. means a series of word characters ending with a "." (so www. and maps. get both found)
- // means "//" (the "" is used as an escape character)
- the middle "|" means either the first part ("\w+.") or the second ("//")
- (?:www.)?
- this agains checkes for an additional "www.", but the ()? means "either 0 or 1 of this". This is used so the new "http://www.youtube.com" test case can be correctly found (since without this iw would find "www.youtube" instead of "youtube"
- (\w+)
- this is the website name, a series of at least one characters
- .\w+
- the ending portion of the website name, a "." followed by a series of characters (like ".com" or ".co.jp")
import re def domain_name(url): return re.findall(r"(?:\w+\.|\/\/)(?:www\.)?(\w+)\.\w+", url)[0]
- import re
- def domain_name(url):
res = re.search(r"(?:(//www\.|//|(?!//)www\.|maps\.))(?P<reqResult>.+?)(?:\.)", url)return res.group(2)- return re.findall(r"(?:\w+\.|\/\/)(?:www\.)?(\w+)\.\w+", url)[0]
Test.assert_equals(domain_name("http://google.com"), "google") Test.assert_equals(domain_name("http://google.co.jp"), "google") Test.assert_equals(domain_name("www.xakep.ru"), "xakep") Test.assert_equals(domain_name("https://youtube.com"), "youtube") Test.assert_equals(domain_name("https://fakewww.com"), "fakewww") Test.assert_equals(domain_name("https://wwwfakewww.com"), "wwwfakewww") Test.assert_equals(domain_name("maps.google.com"), "google") Test.assert_equals(domain_name("http://www.youtube.com"), "youtube")
- Test.assert_equals(domain_name("http://google.com"), "google")
- Test.assert_equals(domain_name("http://google.co.jp"), "google")
- Test.assert_equals(domain_name("www.xakep.ru"), "xakep")
- Test.assert_equals(domain_name("https://youtube.com"), "youtube")
- Test.assert_equals(domain_name("https://fakewww.com"), "fakewww")
- Test.assert_equals(domain_name("https://wwwfakewww.com"), "wwwfakewww")
Test.assert_equals(domain_name("maps.google.com"), "google")- Test.assert_equals(domain_name("maps.google.com"), "google")
- Test.assert_equals(domain_name("http://www.youtube.com"), "youtube")