Extracting Domain 
Author Message
 Extracting Domain

[mailed and posted]
: Can someone suggest an easy way to extract the last two components of
: a hostname.  For example with any of the following:

: machine.net.dom

: machine.subnet.net.dom

: machine.subsub.subnet.net.dom

: is there an easy way to isolate net.dom (consistently grabbing the
: last two components regardless of how many period separators there
: are)?

: Thanks.  Please copy me on any posts if possible.

You might want to check the RFC for the allowable characters in
a top-level domain name.  I believe it is [A-Za-z][-A-Za-z0-9]+, letters,
hyphens, and numbers (after the first, which must be a letter), but
don't take this as gospel.

The regex you need is /([A-Za-z][-\w]*\.[-\w]+)$/.  As a subroutine, since
I need to do this sometimes myself:

            machine.subsub.subnet.net.dom );
sub find_top_domain {



        /([A-Za-z][-\w]+\.[-\w]+)$/;

    }

Quote:
}


$top = find_top_domain('machine.subsub.subnet.net.dom');

print $top, $/;

Regards,
--
Mike Heins                                                    ___       ___
                                    Internet Robotics        |_ _|____ |_ _|
"If you think nobody cares if       131 Willow Lane, Floor 2  | ||  _ \ | |
you're alive, try missing a couple  Oxford, OH  45056         | || |_) || |
of car payments."                                            |___|  _ <|___|



Tue, 05 May 1998 03:00:00 GMT  
 Extracting Domain

Quote:

>Can someone suggest an easy way to extract the last two components of
>a hostname.  For example with any of the following:

A couple of quick solutions off the top of my head:


                        or

        $data = "machine.subsub.subnet.net.dom";
        ($a, $b) = $data =~ m/^.*\.(.*)\.(.*)$/;

Anybody want to clean up that regexp?  In my heart, I know there's a better
way.

Quote:
>Thanks.  Please copy me on any posts if possible.

Certainly.  Hope it helps.

Regards,
--

                URL: http://www.cs.uoregon.edu/~chrissch/
                           University of Oregon



Tue, 05 May 1998 03:00:00 GMT  
 Extracting Domain

 > is there an easy way to isolate net.dom (consistently grabbing the
 > last two components regardless of how many period separators there
 > are)?

Take a look at split(), pop(), and join().  I am deliberately not
demonstrating how to put those three functions together --- it's more
useful for you to figure it out for yourself.  (It'd be pretty easy to
perform the desired task using regular expressions, too, but the
split/pop/join solution is probably a little bit more obvious.)

--

stated, my statements represent my personal viewpoints only.



Tue, 05 May 1998 03:00:00 GMT  
 Extracting Domain

Quote:

> [mailed and posted]
> : Can someone suggest an easy way to extract the last two components of
> : a hostname.  For example with any of the following:
> You might want to check the RFC for the allowable characters in
> a top-level domain name.  I believe it is [A-Za-z][-A-Za-z0-9]+, letters,
> hyphens, and numbers (after the first, which must be a letter), but
> don't take this as gospel.

numbers are allowed as the first character of a top level domain.  Case in
point: 3com.com.

Quote:
> The regex you need is /([A-Za-z][-\w]*\.[-\w]+)$/.  As a subroutine, since
> I need to do this sometimes myself:

However, this this regex does work in the 3com.com case.  Unfortunately,
it also works with something like a_.com, which is not a valid name.

Frank Stutzman
Stutzman Consulting           (on contract to Bay Networks)


http://www.stutzman.com

(my English teach warned me about abusing parentheses)



Tue, 05 May 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. need regex to extract domain.name from host.domain.name

2. extracting domains from whois query

3. extracting domain names

4. Extracting Domain

5. How to extract the toplevel-domain of a domainame from a string with regexp

6. Extracting top level domain from remote host

7. Using domain.com/?query+string and domain.com/index.cgi?query+string...

8. NT Domain Auth and HTTP Headers

9. Adding NT Machine Accounts to a Domain

10. LWP::Simple - Getting Outside of A Local Domain

 

 
Powered by phpBB® Forum Software