Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract just domain names from urls?

I have a following list of URLs:

urls = ["http://arxiv.org/pdf/1611.08097", "https://doi.org/10.1109/tkde.2016.2598561", "https://www.scopus.com/inward/record.uri?partnerID=HzOxMe3b&scp=85116544648&origin=inward"]

from each element of the list, I am trying to extract just the domain names like: arxiv, doi, scopus.

For that I have a code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import re

for url in urls:
    print(re.search('https?://([A-Za-z_0-9.-]+).*', url).group(1))

The output of print:

arxiv.org
doi.org
www.scopus.com

How can I modify the above regex to extract just the domain and no other stuff like www., .com, .org etc?

Thanks in advance.

>Solution :

You can remove the dot from the character class and make www. optional. The value is in capture group 1.

https?://(?:www\.)?([A-Za-z_0-9-]+)

Regex demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading