in reply to extract a hostname from a fqdn
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 ----------------------------------------------------------------------
|
|---|