我有像
这样的字符串 CASE: 8
Location: smth
Destination: 183, 3921, 2.293e-2, 729, 9
END_CASE
我需要将CASE(8)和Destination参数的数量放入变量......怎么做?
答案 0 :(得分:1)
这是regexp:
my $str = "CASE: 8
Location: smth
Destination: 183, 3921, 2.293e-2, 729, 9
END_CASE
";
my ($case,$dest) = $str= m!\A\s*CASE:\s*(\d+).+?Destination:\s*(.+?)\n!gis;
print "case: $case, dest: $dest\n";
编辑:
如果您想匹配多行正则表达式,并且您的文件较小,则可能会玷污它。
如果它更大,那么你可以用块(块)处理它。
啜食:
local $/=undef;
open(my $fh,'<',...) or die $!;
my $str = <$fh>;
while ($str= m!\A\s*CASE:\s*(\d+).+?Destination:\s*(.+?)\n!is){
print "case: $1, dest: $2\n";
}
以块的形式处理:
my $str;
while( my $line = <$fh>){
if ($line !~ m!END_CASE!){
$str .= $line;
} else {
$str .= $line;
### process $str
my ($case,$dest) = $str= m!\A\s*CASE:\s*(\d+).+?Destination:\s*(.+?)\n!gis;
print "case: $case, dest: $dest\n";
### reset chunk
$str = '';
}
}
此致