what is the regex for printing only the domain from a url? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is the regex for printing only the domain from a url?

i have to do it with the use of regex only.. if i give the input as : http://www.google.com The output should be : www.google.com

6th Jun 2018, 4:14 PM
AISHWARYA KASTHALA
AISHWARYA KASTHALA - avatar
6 Answers
+ 13
import re pattern = 'http[s]?://((?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)' url ='https://www.sololearn.com/Discuss/1329933/?ref=app' print(re.findall(pattern, url)) #Pattern Source: http://www.noah.org/wiki/RegEx_Python
6th Jun 2018, 5:39 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 6
Mert Yazıcı congratulations for level 16!
14th Jun 2018, 12:41 PM
Marat Khudayberdin
Marat Khudayberdin - avatar
+ 4
You could do it with string slicing: x = "mmmmm.aaaaa@sololearn.com" print(x[x.index("@") + 1::])
6th Jun 2018, 4:38 PM
Paul
Paul - avatar
+ 2
i have to do it with the use of regex only.. if i give the input as : http://www.google.com The output should be : www.google.com
6th Jun 2018, 4:40 PM
AISHWARYA KASTHALA
AISHWARYA KASTHALA - avatar
+ 1
https://stackoverflow.com/questions/29061521/regular-expression-to-match-only-domain-from-url
6th Jun 2018, 4:21 PM
Louis
Louis - avatar
0
(?<=:\/\/)?([\w]+(?:\.\w+)+) First group is the url. http:// and www. are optional, weird top level domain names like .co.uk also pass this. File path is ignored. Also put escapes before symbols if Python needs it, I don't know Python myself.
6th Jun 2018, 7:05 PM
BlazingMagpie
BlazingMagpie - avatar