我有一个文件数组。每个文件都有几行文本,其中我试图通过perl中的正则表达式获取少量特定字符串
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 :(得分:1)
你可以使用这样的正则表达式:
(good.*|bad.*)
<强> Working demo 强>
匹配信息
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%`
使用上面的正则表达式,您可以捕获所需的行。然后你必须添加一些逻辑来生成你想要的输出。
答案 1 :(得分: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%