in reply to Matching RFC1912 compliant hostnames

(Ignore this... go straight to the pointers by tye)

Here's something quick, until someone comes with a better one:

perl -ne 'print "YES\n" if /^[a-z0-9][a-z0-9.-]*[a-z0-9]$/ && /[a-z]/ +&& ! /\.\./'
(This doesn't accept 1-letter names, which is probably okay)

update: the original one accepted a..b, now it doesn't (and is less elegant :)

Replies are listed 'Best First'.
Re: Re: Matching RFC1912 compliant hostnames
by Fastolfe (Vicar) on Nov 18, 2000 at 05:43 UTC
    It might be easier (well, more readable anyway) to break it up into components:
    my $alpha = 'a-zA-Z'; my $alphanum = $alpha.'\d'; my $any = $alphanum.'-'; my $label = qr/ (?:[$alpha]| # start with a letter \d[$any]*[$alpha]+) # or a number if there's a letter + elsewhere (?: [$any]* # followed by more stuff maybe [$alphanum] # as long as it doesn't end with +a - )? /x; $is_valid = /^(?:$label\.)*$label$/;
    Note that while this matches 1912 hostnames, some newer hostnames have been belatedly declared "valid" though they do not adhere to this RFC (specifically, domains like 411.com or 800.com, which violate the "labels must not be all numbers" rule). Perhaps there is a newer RFC that supercedes 1912?

    Perhaps somebody has a better solution...

      "I was married to a regex, now its my ex-reg"