我有这样的字符串
Modified files: ['A', 'B']
File: /tpl/src/vlan/VlanInterfaceValidator.cpp
Newly generated warnings:
A has warnings
B has warning
Status: PASS
我想要"新生成的警告:"应该是
A has warnings
B has warning
我是perl新手,不知道如何在perl中使用正则表达式。请帮助。
有两个选项:
- 将字符串分成行,并使用grep 过滤行数组
- 在多行字符串 上使用正则表达式
my $str = "
Modified files: ['A', 'B']
File: /tpl/src/vlan/VlanInterfaceValidator.cpp
Newly generated warnings:
A has warnings
B has warning
Status: PASS";
my @lines = grep{ /w+ has warning/ } split(/n/, $str);
print "Option 1 using split and grep:n";
print join("n", @lines);
$str =~ s/^.*Newly generated warnings:s*(.*?)s+Status:.*$/$1/sm;
print "nnOption 2 using regex:n";
print $str;
输出:
Option 1 using split and grep:
A has warnings
B has warning
Option 2 using regex:
A has warnings
B has warning
选项1的解释:
split(/n/, $str)
-将字符串拆分为字符串数组grep{ /w+ has warning/ }
-使用grep正则表达式对感兴趣的行进行过滤- 注意:这是标准正则表达式测试
$_ =~ /w+ has warning/
的缩写。$_
包含字符串元素,例如line.
- 注意:这是标准正则表达式测试
选项1的说明:
$str =~ s/search/replace/
-对字符串进行标准搜索和替换- 注意:与许多其他语言不同,Perl 中的字符串是可变的
s/^.*Newly generated warnings:s*(.*?)s+Status:.*$/$1/sm
:- 搜索:
^.*
-从字符串开始抓取所有内容直到:Newly generated warnings:
s+
-扫描空白(.*?)
-捕获组1与非贪婪扫描s+Status:.*$
-扫描空格,Status:
和其他所有内容以结束字符串
- 替换:
$1
-使用捕获组1
- 标记:
s
-点匹配换行符m
-多行,例如^
是字符串的开始,$
是字符串的结束
- 搜索:
这种问题就是你可以读到你想要的部分的那一行然后不做任何处理,然后读到你想要的部分的开头,保留这些行
# ignore all these lines
while( <DATA> ) {
last if /Newly generated warnings/;
}
# process all these lines
while( <DATA> ) {
last if /As*z/; # stop of the first blank line
print; # do whatever you need
}
__END__
Modified files: ['A', 'B']
File: /tpl/src/vlan/VlanInterfaceValidator.cpp
Newly generated warnings:
A has warnings
B has warning
Status: PASS
从文件句柄中读取。处理字符串非常简单,因为您可以在字符串上打开文件句柄,这样您就可以逐行处理字符串:
my $string = <<'HERE';
Modified files: ['A', 'B']
File: /tpl/src/vlan/VlanInterfaceValidator.cpp
Newly generated warnings:
A has warnings
B has warning
Status: PASS
HERE
open my $fh, '<', $string;
while( <$fh> ) {
last if /Newly generated warnings/;
}
while( <$fh> ) {
last if /As*z/;
print; # do whatever you need
}