2014-09-18 53 views
0

我有一个文件数组。用的几行文字的每个文件,在外面我试图打通的regex几个特定字符串在Perlperl:读取文件时的正则表达式

use strict; 
use warnings; 

foreach my $myfile (@myFiles) { 
    open my $FILE, '<', $myfile or die $!; 
    while (my $line = <$FILE>) { 
     my ($project, $value1, $value2) = <Reg exp>, $line; 
     print "Project : $1 \n"; 
     print "Value1 : $2 \n"; 
     print "Value2 : $3 \n"; 
    } 
    close(FILE); 
} 

*文件内容*

Checking Project foobar 
<few more lines of text here> 
Good Files excluding rules:  15 - 5% 
Bad Files excluding rules: 270 - 95% 

<one more line of text here> 
Good Files including rules:  15 - 5% 
Bad Files including rules: 272 - 95% 
<few more lines of text here> 

*所需的输出*

Project:foobar 
Value1 : Good Files excluding rules:  15 - 5% 
      Bad Files excluding rules: 270 - 95% 
Value2 : Good Files including rules:  15 - 5% 
      Bad Files including rules: 272 - 95% 
+0

'打开我的$ FILE, '<',$ MYFILE死$';;缺少$ myfile'和'die'之间'一个'或'! – 2014-09-18 20:23:01

+0

@JimDavis谢谢,更新 – Jill448 2014-09-18 20:48:08

+0

这些行是严格的顺序,如“好/坏排除”,然后“好/坏包括”,或者他们是否有序和可能交错?另外,我还没有跟上Perl,这是',$ line'一个新的构造? – sln 2014-09-18 21:44:32

回答

1

这是不值得尝试创建一个单一的正则表达式来捕获所有你想要的值。

取而代之,只需逐行处理,然后为每个要匹配的行类型创建一个正则表达式。

use strict; 
use warnings; 

my $fh = \*DATA; 

my $counter = 0; 

while (<$fh>) { 
    if (/Checking Project (\w+)/) { 
     printf "Project:%s\n", $1; 

    } elsif (/^Good Files/) { 
     printf "Value%-2s: %s", ++$counter, $_; 

    } elsif (/^Bad Files/) { 
     printf "  : %s", $_; 
    } 
} 

__DATA__ 
Checking Project foobar 
<few more lines of text here> 
Good Files excluding rules:  15 - 5% 
Bad Files excluding rules: 270 - 95% 

<one more line of text here> 
Good Files including rules:  15 - 5% 
Bad Files including rules: 272 - 95% 
<few more lines of text here> 

输出:

Project:foobar 
Value1 : Good Files excluding rules:  15 - 5% 
     : Bad Files excluding rules: 270 - 95% 
Value2 : Good Files including rules:  15 - 5% 
     : Bad Files including rules: 272 - 95% 
1

您可以使用类似这样的正则表达式:

(good.*|bad.*) 

Working demo

enter image description here

匹配信息

MATCH 1 
1. [54-95] `Good Files excluding rules:  15 - 5%` 
MATCH 2 
1. [96-136] `Bad Files excluding rules: 270 - 95%` 
MATCH 3 
1. [167-208] `Good Files including rules:  15 - 5%` 
MATCH 4 
1. [209-249] `Bad Files including rules: 272 - 95%` 

使用上述正则表达式,你可以捕捉你所需要的线。然后你必须添加一些逻辑来产生你想要的输出。