Perl Regex New Line and no Chartial
我正在尝试从输入文件中提取多行,以输出文件线仅使用Perl中的Regex包含头。逻辑是在数组中添加令牌,然后将traverse Array添加为头部。在添加阵列中的令牌时,添加了匹配的正则匹配的正则表达式新线条,没有字符。
my @arr = split("\n",$str);
foreach my $token (@arr) {
print "Inside for\n";
if($token =~ m[head])
{
print "Inside if";
print $token;
}
}
**File Content**
**InputFile.txt**
- text1
- text2
- head
- text4
- text5
- non head
- text8
- text9
- head
**OutputFile.txt**
- text1
- text2
- head
- text8
- text9
- head
I am trying extract multiple line from an Input file to Output file lines only containing head using regex in perl. Logic is to add lines as token in an array and then traverse array for head. Got stuck matching regex pattern new line and no character while adding lines as token in array.
my @arr = split("\n",$str);
foreach my $token (@arr) {
print "Inside for\n";
if($token =~ m[head])
{
print "Inside if";
print $token;
}
}
**File Content**
**InputFile.txt**
- text1
- text2
- head
- text4
- text5
- non head
- text8
- text9
- head
**OutputFile.txt**
- text1
- text2
- head
- text8
- text9
- head
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以在段落中读取输入( -00 switch> -00 switch )实际上总是像那样的空白,如果它具有(在这种情况下为结束),则打印一个段落,
我使用
\ z
断言但是$
在给定示例中也很好。在脚本中,这是通过设置输入记录分隔仪
我们想要 local ,请不要为整个运行中的所有内容更改
$/
(在较大的程序中)。评论将问题扩大了一点,以便
在段落中读取文件时容易容纳的行 - 只需更改正则义务,以便在任何地方都匹配模式/线(不仅在结尾),这样的话,
如果线处于末尾,仍然可以匹配。
Can read input in paragraphs (-00 switch), if there is really always a blank like there, and print a paragraph if it has (ends with in this case) a desired pattern
I use the
\z
assertion but$
is fine as well in the given example.In a script this is done by setting the input record separator
We want local so to not change
$/
for everything in the whole run (in a larger program).A comment expands the question a little so that
This is simple to accommodate when a file is read in paragraphs -- just change the regex so that it matches a pattern/line anywhere (not only at the end), like
This still matches if the line is at the end, as well.
逐行阅读文件时,可以使用“滑动窗口”技术实现预期的输出。
The expected output can be achieved using the "sliding window" technique when reading the file line by line.