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

Hi there, I am fairly new to PERL, just the intro course under my belt. I use the Regex coach for help with this, but my output is not as I expect. I am reading lines like the one below and am trying to extract the three prices into $1, $2 and $3. "18-December-09";"$1,104.50 ";"29-July-97";"$326.55 ";"25-April-85";"$322.50 "; Any and all help appreciated.

while(<$in_fh>) { my $line = $_; if ( $line =~ m[(\$\d?,?\d\d\d\.\d\d\b){3}] ) { print "$1"; print "$2"; print "$3"; $ctr ++; }

Replies are listed 'Best First'.
Re: why is my regex not right?
by toolic (Bishop) on Jul 18, 2011 at 13:59 UTC
    Just grab all the prices into an array, then use the ones you want:
    use warnings; use strict; use Data::Dumper; while (<DATA>) { my $line = $_; my @prices = $line =~ m[(\$\d?,?\d\d\d\.\d\d\b)]g; print Dumper(\@prices); } __DATA__ "18-December-09";"$1,104.50 ";"29-July-97";"$326.55 ";"25-April-85";"$ +322.50 "
    Prints:
    $VAR1 = [ '$1,104.50', '$326.55', '$322.50' ];
    See Global matching
Re: why is my regex not right?
by JavaFan (Canon) on Jul 18, 2011 at 13:55 UTC
    You're trying to match three amounts in a row. The string does not contain that.

    You either may want to use the /g modifier, or match the dates and the quotes as well.

Re: why is my regex not right?
by Jim (Curate) on Jul 18, 2011 at 18:41 UTC

    I think this regular expression pattern is probably more suitable.

        (\$\d{1,3}(?:,\d\d\d)*\.\d\d\b)
    

    use strict; use warnings; use English qw( -no_match_vars ); ... while (my $record = <$input_fh>) { my @prices = $record =~ m/(\$\d{1,3}(?:,\d\d\d)*\.\d\d\b)/g; $number_of_prices = scalar @prices; $number_of_prices == 3 or warn "Incorrect number of prices ($number_of_prices) at" . " line $INPUT_LINE_NUMBER of file $input_file\n"; ... } exit 0;

    But you should probably use Text::CSV_XS instead of a regular expression pattern to parse the semicolon-separated-value records properly and to extract the prices from them that way.

    Jim