need regex to extract domain.name from host.domain.name 
Author Message
 need regex to extract domain.name from host.domain.name

I'm struggling to find the right expression to extract only the domain name
from a string that contains the host name as well.

In a file of records of host.domain.names, where som have host names, and
some don't!

like

ns1.domain.com
domain0.com
domain2.com
xxx.domain3.com

I need to grab only the

  xxxx.com part

Could some kind soul help please.

Thanks heaps
Robert



Wed, 18 Jun 1902 08:00:00 GMT  
 need regex to extract domain.name from host.domain.name
Robert,

Quote:

> I'm struggling to find the right expression to extract only the
> domain name from a string that contains the host name as well. In
> a file of records of host.domain.names, where som have host names,
> and some don't! like
> ns1.domain.com
> domain0.com
> domain2.com
> xxx.domain3.com
> I need to grab only the
> xxxx.com part
> Could some kind soul help please.

  Since the host name is the first sequence of non-'.' characters, an
easy way is to use the split function:

        print ((split /\./,$fully_qualified_domain_name)[0])

        Best regards,
                Marco

Quote:

> Thanks heaps
> Robert



Wed, 18 Jun 1902 08:00:00 GMT  
 need regex to extract domain.name from host.domain.name

Quote:

> I'm struggling to find the right expression to extract only the domain name
> from a string that contains the host name as well.
> In a file of records of host.domain.names, where som have host names, and
> some don't!
> I need to grab only the
>   xxxx.com part

Group the match with ()'s and match the end of the line

 s%(.*)\.(.*)\.(.*)$%$2\.$3%;

$ perl -nle '$_ =~ s%(.*)\.(.*)\.(.*)$%$2\.$3%; print "Result: $_"'
a.b
Result: a.b
a.b.c
Result: b.c
a.b.c.d
Result: c.d
a.b.c.d.e
Result: d.e

Regards,
        _Gus

--

0x58E18C6D
82 AA 4D 7F D8 45 58 05  6D 1B 1A 72 1E DB 31 B5
http://black.hole-in-the.net/gus/



Wed, 18 Jun 1902 08:00:00 GMT  
 need regex to extract domain.name from host.domain.name
That looks like my baby..

Thanks Gus.

thanks to all the others too.
much appreciated.
Bob


Quote:

> > I'm struggling to find the right expression to extract only the domain
name
> > from a string that contains the host name as well.

> > In a file of records of host.domain.names, where som have host names,
and
> > some don't!

> > I need to grab only the

> >   xxxx.com part

> Group the match with ()'s and match the end of the line

>  s%(.*)\.(.*)\.(.*)$%$2\.$3%;

> $ perl -nle '$_ =~ s%(.*)\.(.*)\.(.*)$%$2\.$3%; print "Result: $_"'
> a.b
> Result: a.b
> a.b.c
> Result: b.c
> a.b.c.d
> Result: c.d
> a.b.c.d.e
> Result: d.e

> Regards,
> _Gus

> --

> 0x58E18C6D
> 82 AA 4D 7F D8 45 58 05  6D 1B 1A 72 1E DB 31 B5
> http://black.hole-in-the.net/gus/



Wed, 18 Jun 1902 08:00:00 GMT  
 need regex to extract domain.name from host.domain.name

Quote:

>ns1.domain.com
>domain0.com
>domain2.com
>xxx.domain3.com

>I need to grab only the

>  xxxx.com part


    my($domain) = /([a-z0-9-]+\.(?:com|net|org|gov|edu|mil))$/i;
    # do something with $domain

Quote:
}

Note the parentheses around $domain, which cause the regex to be
evaluated in a list context.  Note also that this will not work
for host names in country-code top-level domains (.uk, .fr, .jp,
etc.), because many of those TLDs have deeper hierarchies.  For
example, applying a similar regex to .uk host names would result
in assigning all commercial hosts to co.uk, regardless of their
company name, and assigning all academic hosts to ac.uk,
regardless of their university.

--

Washington, DC

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Determine domain name of host?

2. extracting domain names

3. Regex for domain names

4. need to connect and translate domain number to name

5. Porter Needed for NT Perl Domain Name/WebSite Checker Script

6. Extracting top level domain from remote host

7. Hosting companies, find all domains that are hosted

8. sort URL or domain names

9. Domain Name Transferring With CGI and *NOT Java Script

10. Domain Name Investment

11. recover domain name

12. Q: how to match domain name

 

 
Powered by phpBB® Forum Software