Dart extract host from URL string

2,611

You do not need to make use of RegExp in this case.

Dart has a premade class for parsing URLs:

Uri

What you want to achieve is quite simple using that API:

final urlSource = 'https://www.wikipedia.org/';

final uri = Uri.parse(urlSource);
uri.host; // www.wikipedia.org

The Uri.host property will give you www.wikipedia.org. From there, you should easily be able to extract wikipedia.

Uri.host will also remove the whole path, i.e. anything after the / after the host.

Extracting the second-level domain

If you want to get the second-level domain, i.e. wikipedia from the host, you could just do uri.host.split('.')[uri.host.split('.').length - 2].

However, note that this is not fail-safe because you might have subdomains or not (e.g. www) and the top-level domain might also be made up of multiple parts. For example, co.uk uses co as the second-level domain.

Share:
2,611
SLendeR
Author by

SLendeR

Updated on December 26, 2022

Comments

  • SLendeR
    SLendeR over 1 year

    Supposing that I have the following URL as a String;

    String urlSource = 'https://www.wikipedia.org/';
    

    I want to extract the main page name from this url String; 'wikipedia', removing https:// , www , .com , .org part from the url.

    What is the best way to extract this? In case of RegExp, what regular expression do I have to use?

  • MonkeyZeus
    MonkeyZeus over 3 years
    OP didn't ask about extracting the host.
  • creativecreatorormaybenot
    creativecreatorormaybenot over 3 years
    @MonkeyZeus I added an explanation - retrieving the second-level domain is actually not strictly always possible w/o a map of all top-level domains. Therefore, my simple example should do. However, it is really trivial from there..
  • MonkeyZeus
    MonkeyZeus over 3 years
    Unless OP is dealing with a customized DNS or localhost then I would imagine the TLD is guaranteed for public-facing websites so you do not need to care about what it is but rather retrieve the second to last string when splitting by period.
  • creativecreatorormaybenot
    creativecreatorormaybenot over 3 years
    @MonkeyZeus I agree. It is so trivial that the answer is probably more useful if kept more generic.
  • MonkeyZeus
    MonkeyZeus over 3 years
    I'm not familiar with dart but a one-liner could be uri.host.split('.')[uri.host.split('.').length - 2]