2016-08-24 10 views
0

と2番目のインスタンスを解析I為替レートを取得するコードを持っている: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