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

hi monks i got problem with the following code
$w = "host, my-host-name # My Host Name";
i want to parse the "my-host-name" in a single scalar variable how do i do it i tried it this way but it didnt work
($x) = $w =~ /^host,\s*(\w+)\s*/;
but the $x contained only "my". i know we can parse it like this
($x) = $w =~ /^host,\s*(\w+-\w+-\w+)\s*/;
but the problem is the name "my-host-name" is not always the same way it can also be "myname" or "my_host-name" etc

Janitored by Arunbear - added code tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: probelm with parsing regex
by borisz (Canon) on Nov 11, 2004 at 11:44 UTC
    Add the missing chars like - to your expression.
    ($x) = $w =~ /^host,\s*([\w-]+)\s*/;
    Boris
Re: problem with parsing regex
by Happy-the-monk (Canon) on Nov 11, 2004 at 11:44 UTC

    Use a character class containing both \w and "-":

    /^host,\s*([\w-]+)/

    Cheers, Sören