2016-08-24 69 views
0

我有代碼獲得的匯率:Perl的解析第二實例與正則表達式

#!/usr/bin/perl 
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!"); 
} 

我怎樣才能抓住第二實例/ \ S +(\ d,\ d {4})/ GI ??

我試圖從Perl的食譜這樣的解決方案:

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

但是我有錯誤:

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

回答

1

分配模式匹配操作的結果到一個數組。該陣列將包含來自所有匹配的所有捕獲組:

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 
+0

它的工作。謝謝 – Hesperson