however it is possible that the fqdn is simply a hostname

Basic debugging checklist say you should give an example, cause the way I understand it, hostname/fqdn/domain are all the same thing

Add use re 'debug'; in a short file like this to see how the engine matches the regex

for( 'hd_defect-onomatopeia', 'hd_defect-host.foo.bar.com' ){ if( m{hd_defect-( ([^\.]+$) | (?: ([^\.]+).(.+$) ) )}sx ){ my $fqdn = $1; my $host = $2; defined $host or $host = $3; $dom = $4; defined $dom or $dom = $fqdn; print "fqdn( $fqdn) host( $host) dom( $dom )\n"; } } __END__ fqdn( onomatopeia) host( onomatopeia) dom( onomatopeia ) fqdn( host.foo.bar.com) host( host) dom( foo.bar.com )

YAPE::Regex::Explain can help to explain simple regex syntax like this

use YAPE::Regex::Explain; print YAPE::Regex::Explain->new( qr{hd_defect-( ([^\.]+$) | (?: ([^\.]+).(.+$) ) )}sx )->explain; __END__ The regular expression: (?sx-im:hd_defect-( ([^\.]+$) | (?: ([^\.]+).(.+$) ) )) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?sx-im: group, but do not capture (with . matching \n) (disregarding whitespace and comments) (case-sensitive) (with ^ and $ matching normally): ---------------------------------------------------------------------- hd_defect- 'hd_defect-' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- [^\.]+ any character except: '\.' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- | OR ---------------------------------------------------------------------- (?: group, but do not capture: ---------------------------------------------------------------------- ( group and capture to \3: ---------------------------------------------------------------------- [^\.]+ any character except: '\.' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \3 ---------------------------------------------------------------------- . any character ---------------------------------------------------------------------- ( group and capture to \4: ---------------------------------------------------------------------- .+ any character (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of \4 ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

In reply to Re: extract a hostname from a fqdn by Anonymous Monk
in thread extract a hostname from a fqdn by iang

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.