2016-09-14 35 views
1

我环顾四周,但找不到一个整洁的工作解决方案。我一直在尝试使用TEXT:CSV_XS,所以这不仅仅是用正则表达式来做一些难事。我可能无法轻松安装TEXT :: CSV,但我确实有XS版本。用混合字符串解析嵌入式引号的CSV

我只需要解析成csv字段,我将稍后分解成kv对。

use Text::CSV_XS; 
use Data::Dumper; 

my $csv = Text::CSV_XS->new ({ allow_loose_quotes => 1, 
           allow_whitespace => 1, 
           eol => $/ }); 

my $str3 = '09/11/2016 22:05:00 +0000, search_name="ThreatInjection - Rule", search_now=1473644880.000, search="bunchof|stuff1,bunch%of-stuff2", count=100'; 

my $status = $csv->parse($str3); 
my @details = $csv->fields(); 
print $csv->error_diag(); 
print Dumper(\@details); 

结果输出是:

$VAR1 = [ 
     '09/11/2016 22:05:00 +0000', 
     'search_name="ThreatInjection - Rule"', 
     'search_now=1473644880.000', 
     'search="bunchof|stuff1', 
     'bunch%of-stuff2"', 
     'count=100' 
    ]; 

因此,要求是让搜索= “bunchof | stuff1,一堆%的-stuff2” 停留在一个领域。我相信答案很简单,但有点难以理解。任何帮助赞赏。

回答

1

你可以使用Text::ParseWords这个标准的Perl发行版永远包含它。

#!/usr/bin/perl 

use strict; 
use warnings; 
use Text::ParseWords; 
use Data::Dumper; 

my $str3 = '09/11/2016 22:05:00 +0000, search_name="ThreatInjection - Rule", search_now=1473644880.000, search="bunchof|stuff1,bunch%of-stuff2", count=100'; 

my @details = parse_line(',\s*', 1, $str3); 

print Dumper \@details; 

输出:

$VAR1 = [ 
      '09/11/2016 22:05:00 +0000', 
      'search_name="ThreatInjection - Rule"', 
      'search_now=1473644880.000', 
      'search="bunchof|stuff1,bunch%of-stuff2"', 
      'count=100' 
     ]; 
+0

感谢您的快速和简单的答案。在一个箱子里我的自我如此之深,我没有想到在另一个箱子里看。 –