Hesperson Hesperson - 2 months ago 6
Perl Question

Perl parse second instance with regex

I have code which get a rate of exchange:

use warnings;
use strict;
use LWP::Simple;
use POSIX qw(strftime);
use Math::Round;
use CGI qw(header start_html end_html);
use DBI;

sub isfloat {
my $val = shift;
return $val =~ m/^\d+.\d+$/;


my $content = get('URL PAGE');
$content =~ /\s+(\d,\d{4})/gi;

my $dolar = $1;
$dolar =~ s/\,/./g;
if (!isfloat($dolar)) {
error("Error USD!");

How can I grab second instance /\s+(\d,\d{4})/gi ??

I tried solution from Perl Cookbook like this:

$content =~ /(?:\s+(\d,\d{4})) {2} \s+(\d,\d{4})/i;

but I have errors:

Use of uninitialized value $val in pattern match (m//)
Use of uninitialized value $dolar in substitution (s///)


Assign the pattern match operator result to an array. The array will contain all capture groups from all matches:

my $content = "abc 1,2345 def  0,9876    5,6789";
my @dollars = $content =~ /\s+(\d,\d{4})/g;

# Now, use the captures in @dollars this way:
foreach my $dollar (@dollars[0,1]) {
    # process the $dollar items in a loop

# ... or this way:
my $dollar1 = shift @dollars;
# process the $dollar1
my $dollar2 = shift @dollars;
# process the $dollar2