Well, Randal put up the hostname verifier, and I made a one-regex solution. This will verify fully qualified domain names, complete with a two, three, or four character specification (like 'com', 'uk', or 'arpa').

Updated... it just wasn't working... there was an extra /\./ in there.
sub verify_FQDN { return $_[0] =~ m{ \A (?: (?! \d+ \. ) [a-zA-Z0-9]+ (?: -+ [a-zA-Z0-9]+ )* \. ) + [a-zA-Z]{2,4} \z }x; }

Replies are listed 'Best First'.
RE: Fully Qualified Domain Name
by extremely (Priest) on Sep 26, 2000 at 14:02 UTC

    Hmm.

    • 411.com - should pass
    • penny-arcade.com - should pass
    • bad-.com - should fail
    • -bad.com - should fail
    • 6-7.m-w.com - should pass
    • 9999999999999999999999999.z.no - should pass
    • 200.100.50.1.uk - should fail
    • 1.com should fail
    • 1.hostile.com should pass
    • 256.com should pass
    The original rule was: [a-zA-Z]([a-zA-Z0-9-]*[a-zA-Z0-9])?

    for each label between dots since that way a program that discriminated between network addresses and host names could simply sniff the first char. That has since be "warped" in the root tables to allow the much more complex well if it doesn't start with a number between 0-255 without any a-zA-Z- or it has only three labels we'll just assume...

    basically, FQDN is just a theory to verify without asking a DNS server. But you SURELY need '-' hyphens in there. and should 1.co.uk match or how about 1.com or 256.com?

    Sorry 'bout this but domain name vs. hostname madness is a particular bone in my throat. Just when I think I've swallowed it, it's choking me again.

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

RE: Fully Qualified Domain Name
by Kanji (Parson) on Sep 26, 2000 at 05:23 UTC
    Prolly nowhere near as efficient, definitely uglier, but it's far more accepting ( perhaps a little too accepting ;), allowing for real-world addresses like 0.168.192.in-addr.arpa, 311.com, private TLDs, and hostnames qualified with trailing periods.
    sub verify_FQDN { return 1 if defined $_[0] && $_[0] =~ m{\A(([a-z0-9]([a-z0-9\-]+)?)?[a-z0-9]\.?)+\Z}i; }
Re: Fully Qualified Domain Name
by Anonymous Monk on Apr 18, 2013 at 18:34 UTC

    To copmly with RFC 1123, I used what was posted here: http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address

    Here's my subroutine for perl (seems to work nicely):

    sub is_FQDN { my $testval = shift(@_); ( $testval =~ m/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9] +)\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/ ) ? return 1 : return 0; }