【发布时间】:2017-02-13 11:12:00
【问题描述】:
我无法在下面的程序中使用范围运算符来查找提供的 2 个文本的开头和结尾之间的文本,即 $kw1 和 $kw2。我正在从名为 oiutput.txt 的文件中读取 $kw1 和 @kw2在这个节目中。 有人可以帮忙说明为什么我的范围运算符没有给出我想要的 o/p 吗?
my $file = "output.txt";
my $kw1 = "Session Initiation Protocol (REGISTER)";
my $kw2 = "CSeq: 3 REGISTER";
open(my $fh, '<', $file) or die $!;
my @content = <$fh>;
#print @content;
foreach (@content) {
#print $kw1;
#print $kw2;
if (/$kw1/ .. /$kw2/) {
print "$_\n";
}
}
我从 Is 搜索的示例数据:
CSeq: 3 REGISTER
Session Initiation Protocol (REGISTER)
Temp
Temp
Session Initiation Protocol (REGISTER)
Session Initiation Protocol (REGISTER)
CSeq: 2 REGISTER
CSeq: 1 REGISTER
CSeq: 0 REGISTER
【问题讨论】:
-
转义
\(REGISTER\)。 -
我仍然在 E:\Automation Related\Perl Scripts - ALL\IMS_debugging\test.pl 第 17 行收到 # Failed test 'data structure should be the same' # 失败。# Structures begin different at: # $got->{j} = '867-5309x' # $expected->{j} = '867-5309' # 看起来你没有通过 1 的 1 测试。
-
可以处理一些您正在匹配的示例数据。
-
另外:我不确定范围运算符在 foreach 循环中是否能正常工作。通常我会在
while ( <$fh> ) {内完成... -
添加了示例数据。用while循环也不例外。
标签: perl