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

Hello monks , I am new to perl and have small question, I am trying to match some number inside a string : the sting is
$string1="version: 0410.1";
and I want to match only 0410.1 from that string , I tried:
$s_version =~ /version:\s([\w]+[\.\w+]*/;
but no luck ,, can someone advice ? thanks

Replies are listed 'Best First'.
Re: matching string
by ikegami (Patriarch) on Aug 23, 2004 at 20:12 UTC

    The only missing in your regexp is the closing ), and you're using different variable names (try use strict;).

    $s_version = "version: 0410.1"; $s_version =~ /version:\s([\w]+[\.\w+]*)/; print("$1\n"); # 0410.1

    However, I recommend using insensate's easier to read expression.

Re: matching string
by insensate (Hermit) on Aug 23, 2004 at 20:02 UTC
    if($string1=~/version: (\d+\.\d+/){ $version=$1; print $version; }
    Should do the trick...
Re: matching string
by ysth (Canon) on Aug 23, 2004 at 23:43 UTC
    I think you are misunderstanding what [ ] do. They form a character class. ( ) or (?:   ) are used for grouping.

    Your regex (with an ending ')' added) literally reads, find "version:" followed by one whitespace character followed by one or more word characters (digits, letters, or underscore), followed by zero or more characters that are either . or a word character. This is equivalent to the simpler regex /version:\s(\w[.\w]*)/, but I have the feeling you are looking for something else; perhaps /version:\s(\d+\.\d+)/ or /version:\s(\d+(\.\d+)?)/.