hello hrcerq,

sorry not to be the right guy to review your regex, but I'd suggest a totally different approach.

As you already extracted rules from the fun RFC plethora and, as you said, there are many complications and dark corners I'd prefere a more verbose but easier to implement and to expand dedicated subroutine or, in ideal world, a dedicated module to do this: and yes perl's ecosystem is nearly an ideal world and we have Data::Validate::Domain but as I read in your homenode: I like to take the most out of core Perl before resorting to external modules. let assume you wont your own solution.. but look carefully at Data-Validate-Domain.t and also to Regexp-Common's plethora of RFC utilities for URIs...

To be precise you must be more explicit on what you wont to validate: hostnames to browse or hostnames for a DNS entry? Infact you mention internatiolised hostname but for first ones you have Unicode and for the latter ACE-strings

..but going on your own, and assuming you are not looking for DNS entries, I'd go with something like

sub validate_hostname{ my $candidate = shift; my ($ascii_only, $verbose, $debug) = @_; # leave room for improvme +nts and flexibility my ($return, $descr); # non ASCII if ( $candidate =~ m/[^[:ascii:]]/ ) { # but see: https://perlmo +nks.org/?node_id=11164574 print "Not ACSII\n" if $verbose; # ..accepted? if ($ascii_only){ return wantarray ? (undef,"non ASCII string [$candidate] r +ejected") : undef; } # go with another specialized sub.. validate_hostname_Unicode($candidate,$ascii_only, $verbose, $d +ebug ); } # ASCII # too long.. if (length $candidate >= 255){ $descr = "[$candidate] is too long (".length $candidate." cha +rs)"; print $descr if $verbose; return wantarray ? (undef,$descr) : undef; } # Hostnames might be composed by 1 or more labels (separated by do +ts) unless ($candidate =~ /\./){ $descr = "[$candidate] contains no dots"; print $descr if $verbose; return wantarray ? (undef,$descr) : undef; } # .. more checks for this rule # Each label may have at most 63 characteres ... #..have fun :) }

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Regex for hostname validation by Discipulus
in thread Regex for hostname validation by hrcerq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.