Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Test the syntactic validity of a domain name

by merlyn (Sage)
on Sep 20, 2000 at 20:51 UTC ( [id://33339]=CUFP: print w/replies, xml ) Need Help??

AltBlue wanted to test a given string to see if was syntactically valid as a domain name. Here's the code that was too long to type into the chatterbox.
sub could_be_a_host { my @a = split /\./, shift, -1; @a > 0 and not grep !/^[a-z0-9\-]+$/i, @a and not grep /^\d+$/, @a and not grep /^-|-$/, @a; }
or, a slightly faster version:
sub could_be_a_host { my @a = split /\./, shift, -1; return 0 unless @a; for (@a) { return 0 unless /^[a-z0-9\-]+$/i; return 0 if /^\d+$/; return 0 if /^-/; return 0 if /-$/; } 1; }
(Modified slightly per tye's suggestion, thanks!)

Replies are listed 'Best First'.
RE: Test the syntactic validity of a domain name
by tye (Sage) on Sep 20, 2000 at 21:01 UTC

    You forgot that split drops trailing null fields by default. For all but the oldest of perls, you can do the following to fix that:

    my @a= split /[.]/, shift, -1;
            - tye (but my friends call me "Tye")
RE: Test the syntactic validity of a domain name
by japhy (Canon) on Sep 20, 2000 at 21:05 UTC
    fixed, updated, whatever...

    Randal, could it be sped up via a "must match this" regex?
    sub valid_domain { return $_[0] =~ m{ \A (?: (?!\d+\.) [a-zA-Z0-9]+ (?: -+ [a-zA-Z0-9]+ )* \. )* (?!\d+\z) [a-zA-Z0-9]+ (?: -+ [a-zA-Z0-9]+ )* }x;


    $_="goto+F.print+chop;\n=yhpaj";F1:eval

      Seemed close but allowed "-" as last char of components.

              - tye (but my friends call me "Tye")
        I fixed it. I guess I should've done update or something like that, as I've seen in other people's posts.

        $_="goto+F.print+chop;\n=yhpaj";F1:eval
RE: Test the syntactic validity of a domain name
by ncw (Friar) on Sep 22, 2000 at 18:00 UTC
    It matches newlines at the end of the domain name :-

    print could_be_a_host("www.domain.com\n"); # says 1
    \z instead of $ in the regexps would fix it for recent perls as would a judicously placed chomp - but I'm sure you know this already!
Re: Test the syntactic validity of a domain name
by extremely (Priest) on Nov 18, 2000 at 13:22 UTC
    Sigh, I hate to party poop this so late but www.411.com is legit and the code above disallows it. More importantly the rules on the third level are a great deal less persnickity than most people think. All kinds of abuses are possible. See DNS Adventure and the legendary URLCalc hack.

    --
    $you = new YOU;
    honk() if $you->love(perl)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://33339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-28 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found