[EMAIL PROTECTED] wrote:
> I'm doing some splitting of URLs, and things are getting more and
> more complicated, what with suddenly needing to allow two, three, or
> even four character TLDs (.us, .gov, .info). I originally thought I'd
> only need to pass a few common threes--such as .com.
>
> Is there a standard Perl library routine (can't use CPAN) that
> divides up URLs? This seems like a common enough task that this
> particular wheel might already exist. All I need to do is split
> something like "foo.bar.com" into "foo" and "bar.com". It's for the
> second bit, the domain, where this issue is suddenly sprouting horns
> and a pointy tail. Oh, it also needs to ignore any trailing thingies
> like "foo.bar.com/ignore/this/".
>
> Current code is:
>
> if ($in =~ /^[^\.]+?\.(com|gov|net|org)$/) {
> $domain = $in;
> } else {
> $in =~ m{^(.*)\.(.+\.(com|gov|net|org)).*$};
> $domain = $2;
> $child = $1;
> }
>
> where $in is the URL, less any prefix. The prefixes, "http://" or
> "https://", are already handled by preceding lines, as are any
> "trailing thingies."
>
> I've thought of using [a-z]{2,4} to replace the list in the regexps,
> but that places a big ol' "CAVEAT EMPTOR" in front of the users--one
> I hope might be avoidable. Plus, I'm afraid of other things popping
> up, like, oh, "dnr.state.wi.us", which is a real URL for Wisconsin
> DNR, or something like "foo.us.gov". Both of which would FAIL the
> current code, as well as the [a-z]{2,4} kludge.
In your example, what is supposed to happen to URLs that don't end in
(com|gov|net|org)?
Since you said that http:// and trailing stuff is previously handled,
I would be tempted to do something like this:
if ($in =~ /^([^\.]+)\.(.*)/) {
$domain = $2;
$child = $1;
} else {
# URL either starts with a period or contains no periods
}
Where is the original data coming from? Is there really any need to
make sure that the top level domain is valid? Even if the domain ends
in ".com", it still may not be valid.
Are you trying to split the URL or validate it? The first is simple,
the second requires DNS queries to do properly.
--
Bowie
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs