how to extract domain name without sub domain from url

Discussion in 'Ruby' started by Chem Leakhina, Jun 23, 2009.

  1. Hi everyone,

    Does anyone know how to extract domain name without sub domain from url?

    Example: =>

    Please give me an example code in ruby.

    Chem Leakhina, Jun 23, 2009
    1. Advertisements

  2. This is actually quite difficult, because there is a multitude of
    possible second-level domains which can be used (such as, and
    they are not really standardized. Just picking one at random, the
    country of Jordan has,,,,,,, and

    If one were to ignore such things, then it becomes easier:

    $ irb
    irb(main):001:0> require 'uri'
    => true
    irb(main):002:0> u = URI.parse ""
    => #<URI::HTTP:0xb7bbf848 URL:>
    => ""
    => ["domain", "com"]
    => ""

    However, as mentioned above, there are a lot of domains this will not
    work for.

    Justin Collins, Jun 23, 2009
    1. Advertisements

  3. We can get better results by ignoring particular known domain prefixes
    such as "ftp" and "www":

    # this works with 1.8 and 1.9
    }.each do |domain|
    dom = domain.sub(/^(?:www|ftp)\./, '')[/^[^.]+/]
    printf "%p -> %p\n", domain, dom
    # alternative
    dom = domain[/^(?:(?:ftp|www)\.)?([^.]+)/, 1]
    printf "%p -> %p\n", domain, dom

    Kind regards

    Robert Klemme, Jun 23, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.