BlueLines has asked for the wisdom of the Perl Monks concerning the following question:
This works, but doesn't look very JAPH-esque. I started to write a regex, but after it got longer than 1 line i gave up (faster development always beats l33ter code). Here's what i started on:$error=1 if ($hostname =~/\.$/); #trailing . is bad my @labels = split(/\./, $good_string); foreach my $foo (@labels) { $error=1 if ($foo=~/^\-/); #can't start with a - $error=1 if ($foo=~/\-$/); #can't end with a - $error=1 if ($foo=~/^\d+$/); #can't be only numeric last if $error; } if ($error) { print "A hostname!\n"; } else { print "Not a hostname!\n"; }
Ugh. The not matching of the "-" at the beginning of a line counts as one match (as does the end), so this failed when the label was shorter than 3 characters. Anyone have an idea on how to implement this in one line?$hostname =~/^[^-]([a-zA-z\-])+[^-](\.[^-][a-zA-Z0-9\-]+?[^-])+?/;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Matching RFC1912 compliant hostnames
by tye (Sage) on Nov 18, 2000 at 06:44 UTC | |
|
Re: Matching RFC1912 compliant hostnames
by fundflow (Chaplain) on Nov 18, 2000 at 05:03 UTC | |
by Fastolfe (Vicar) on Nov 18, 2000 at 05:43 UTC | |
by fundflow (Chaplain) on Nov 18, 2000 at 06:24 UTC |