http://www.perlmonks.org?node_id=1025093

iang has asked for the wisdom of the Perl Monks concerning the following question:

I have to extract a hostname from a fqdn, however it is possible that the fqdn is simply a hostname. so for host.foo.bar.com, I want to split (host,domain). and for host , I want to split, but domain should be null. I realize that I can do this with split, but I wanted to try a regex. My code is like this:
if($file =~ m/hd_defect-(.*?)\.(.*)$/) { $host = $1; $dom = $2; }
this does not work for a simple host, so I added a modifier:
if($file =~ m/hd_defect-(.*?)\.?(.*)$/) { $host = $1; $dom = $2; }
But that does not work either. Any pointers would be appreciated. Thanks

Replies are listed 'Best First'.
Re: extract a hostname from a fqdn
by Anonymous Monk on Mar 23, 2013 at 23:54 UTC

    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 ----------------------------------------------------------------------
Re: extract a hostname from a fqdn
by CountZero (Bishop) on Mar 24, 2013 at 07:32 UTC
    Have a look at ParseUtil::Domain.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: extract a hostname from a fqdn
by rjt (Curate) on Mar 23, 2013 at 23:46 UTC
    /^ ([^.]+) (?:\.(.+))? $/x; say "Host: `$1', domain: `$2'";
Re: extract a hostname from a fqdn
by McA (Priest) on Mar 24, 2013 at 06:52 UTC

    Hi,

    knowing it's more a question about perl regex I just want to remember that there is a core module Net::Domain giving solutions in the mentioned problem domain.

    Update: I've to correct myself. This module gives you the mentioned informations for the currenct host, but doesn't extract these informations from a given string representing domain names.

    McA