Perl Regex不匹配

时间:2012-07-25 15:31:16

标签: regex perl

我正在尝试匹配文件中的行并提取某个部分。 我的正则表达式适用于我能找到的所有在线测试人员,但不适用于我的perl。 我的版本是v5.10.0,无法更新。

正则表达式看起来像这样:

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+.+?\_.+$/) {

4 个答案:

答案 0 :(得分:2)

测试:当if ($file !~ "0") {在任何位置都不包含$file时,0为真,这只是最后一个字符串的情况。

我想您要使用:if ($file ne '0') {甚至更短:if ($file) {

除此之外,您应该始终use strict;use warnings

答案 1 :(得分:0)

你想要匹配什么?最后一部分? 如果您知道有什么东西可以匹配,请不要使用*,而是使用+:

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

答案 2 :(得分:0)

我猜你的测试文件的最后一行是唯一一个不以“\ n”结尾的行。有趣的小虫子总是挡路......

答案 3 :(得分: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);     
    } 
}