2012-07-25 179 views
2

我想匹配文件中的行並提取某個部分。 我的正則表達式可以與我所能找到的所有在線測試人員一起工作,但不能與我的Perl一起工作。 我在版本v5.10.0,無法更新。Perl正則表達式不匹配

正則表達式如下:

sub parse_bl_line { 
    if ($_[0] =~ m/^copy\s+.*?\s+(.*?\_.*)/) { 
      return $1; 
    } else { 
      log_msg("Line discarded: $_[0]", 4); 
    return "0"; 
    } 

}

應符合(只剩下最後的比賽)的測試數據的幾行:

@bl_lines = (
"copy xxxxxx_/cpu  b_relCAP_R3.0-1_INT5_xxxxx_cpu_p1", 
"copy xxxxxxxx_/va_xxx_parameters b_relCAP_R3.0-1_INT5_xxxxx_va_xxx_parameters_p1", 
"copy xxxxxxxx_/xxxxxxx_view.tcl  b_relCAP_R3.0-1_INT5_xxxxxx_view.tcl_p0", 
"copy xxxxx_/xxxxxarchivetool.jar b_relEARLY_DROP1_xxxxxarchivetool.jar_xx"); 

並調用函數:

foreach(@bl_lines) { 
    $file=parse_bl_line($_); 
    if ($file !~ "0") { 
      log_msg("Line accepted: $_", 4); 
      log_msg("File extracted: $file", 4); 
    }else { 
      log_msg("Line rejected: $_", 2); 
    } 

}

我試圖匹配最後一部分,例如

b_relEARLY_DROP1_xxxxxarchivetool.jar_xx 

輸出看起來如下:

20120726 13:15:34 - [XXX] ERROR: Line rejected: copy xxxxxx_/cpu  b_relCAP_R3.0-1_INT5_xxxxx_cpu_p1 
20120726 13:15:34 - [XXX] ERROR: Line rejected: copy xxxxxxxx_/va_xxx_parameters b_relCAP_R3.0-1_INT5_xxxxx_va_xxx_parameters_p1 
20120726 13:15:34 - [XXX] ERROR: Line rejected: copy xxxxxxxx_/xxxxxxx_view.tcl  b_relCAP_R3.0-1_INT5_xxxxxx_view.tcl_p0 
20120726 13:15:35 - [XXX] INFO: Line accepted: copy xxxxx_/xxxxxarchivetool.jar b_relEARLY_DROP1_xxxxxarchivetool.jar_xx 
20120726 13:15:35 - [XXX] INFO: File extracted: b_relEARLY_DROP1_xxxxxarchivetool.jar_xx 

提示 我做了一些@BaL提出並發現該模式匹配作品,未經選擇括號中的測試。

if ($_[0] =~ m/^copy\s+.+?\s+.+?\_.+$/) { 
+0

你如何設置'$ _ [0]'? – sergio 2012-07-25 15:42:46

+1

什麼*你的正則表達式匹配?你可以交換'返回1美元'爲'說$ 1'並且告訴我們測試輸入的輸出嗎?請參閱下面的@ Bal的評論。 – amon 2012-07-25 20:42:43

+0

[適用於我!](http://codepad.org/o2nD2xJu)錯誤不在這裏。顯示你的整個代碼**。 – daxim 2012-07-26 09:46:46

回答

2

測試:當$file不會在其僅最後一個字符串的情形中的任何位置包含一個0if ($file !~ "0") {是真實的。

我猜你想使用:if ($file ne '0') {甚至更​​短:if ($file) {

除了這個你真的應該use strict;use warnings始終。

0

你想匹配什麼?最後一部分? 不要使用*如果你知道你有什麼要匹配,使用+代替:

if ($_[0] =~ m/^copy\s+.+?\s+(.\+?)$/) { 
    return $1; 
} 
+0

這沒有幫助,不幸的是,相同的結果 – 2012-07-25 15:55:29

+2

如果你匹配以下內容後打印出「$ 1」會發生什麼:'$ _ [0] =〜m/^ copy(。+)$/',然後 '$ _ [0] =〜m/^ copy \ s +(。+)$ /',那麼 '$ _ [0] =〜m/^ copy \ s +。+(。+)$/'等,直到你找到你的模式出錯的地方? – BaL 2012-07-25 16:50:18

0

我猜您的測試文件的最後一行是不以結束僅一個「\ n」。有趣的小辮子總是擋道.....

+0

這不是問題,我正在使用與它自己的每一行匹配的測試。 – 2012-07-25 15:44:34

0

當你正在進行字符串比較時,將你的if語句中的比較運算符從!〜更改爲ne。當我進行此更改時,所有日誌行都被接受。

我在perl 5.14.2上測試了這個,而不是5.10,但我沒有使用任何特殊功能。搏一搏!代碼如下:

use 5.14.2; 

sub log_msg{ 
    say shift; 
} 

sub parse_bl_line { 
    if ($_[0] =~ m/^copy\s+.*?\s+(.*?\_.*)/) {    
     return $1;  
    } 
    else {    
     log_msg("Line discarded: $_[0]", 4);  
     return "0";  
    } 
} 

my @bl_lines = ( 
    "copy xxxxxx_/cpu     b_relCAP_R3.0-1_INT5_xxxxx_cpu_p1", 
    "copy xxxxxxxx_/va_xxx_parameters b_relCAP_R3.0-1_INT5_xxxxx_va_xxx_parameters_p1", 
    "copy xxxxxxxx_/xxxxxxx_view.tcl  b_relCAP_R3.0-1_INT5_xxxxxx_view.tcl_p0", 
    "copy xxxxx_/xxxxxarchivetool.jar b_relEARLY_DROP1_xxxxxarchivetool.jar_xx" 
); 

foreach(@bl_lines) {  
    my $file = parse_bl_line($_);  
    if ($file ne "0") { # Changed the comparison operator here   
     log_msg("Line accepted: $_", 4);    
     log_msg("File extracted: $file", 4); 
    } 
    else {    
     log_msg("Line rejected: $_", 2);  
    } 
}