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 )