2016-09-14 36 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

感謝您的快速和簡單的答案。在一個箱子裏我的自我如此之深,我沒有想到在另一個箱子裏看。 –