从perl中的字符串中删除多个子字符串

时间:2017-04-21 09:23:40

标签: perl

我的脚本如下所示。

my $temp='abc.ifb "BATCHRANGE=110000198" def'; 
my $temp1=reverse $temp;    
my $temp2=substr($temp1,0,index($temp1,'"'));    
my $task=reverse $temp2;    
my $task1=substr($task,1);    
print "\n my task is $task1 \n";    
my $temp3 = '.ifb';
my $ifb = substr($temp, 0, index($temp, $temp3)+4);    
print "\n my ifb is $ifb \n";    
$temp =~ s/$ifb //;    
$temp =~ s/$task1 //;    
print "\n now temp is $temp \n";

实际上我想分别捕获三个字符串。但最终的印刷品显示“BATCHRANGE = 110000198”def。而我期待“BATCHRANGE = 110000198”。这有什么不对?

1 个答案:

答案 0 :(得分:1)

$task1 s/$task1 //之后您有空格有什么不对。{/ 1}}。

使用正则表达式的解决方案:

my $temp='abc.ifb "BATCHRANGE=110000198" def'; 
my($ifb, $temp, $task);
if ($temp =~ m/^(.+\.ifb)\s+("[^"]*")\s+(.+)$/) {
    ($ifb, $temp, $task) = ($1, $2, $3);
    print "My ifb is '$ifb'\n";
    print "My temp is '$temp'\n";
    print "My task is '$task'\n";
}
else {
    print "Wrong format\n";
}

=>

My ifb is 'abc.ifb'
My temp is '"BATCHRANGE=110000198"'
My task is 'def'