Perl/regex 删除字符串的前 3 行和后 3 行 [英] Perl/regex to remove first 3 lines and last 3 lines of a string

查看:58
本文介绍了Perl/regex 删除字符串的前 3 行和后 3 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望构建一个正则表达式来始终删除前 3 个字符串的行,以及字符串的最后 3 行(中间部分可以是任意 n 行内容).任何干净的正则表达式方式来实现这个输出?(即总是去掉我们的前 3 行和后 3 行字符串 - 并保留中间部分,这可能是一个变量 #行)

I was looking to build a regex statement to always remove the first 3 lines of the string, and last 3 lines of the string (the middle part could be any n number of lines content). Any clean regex way to acheive this output? (i.e. always strip our first 3 lines and last 3 lines of the string - and preserve the middle part, which could be a variable # of lines)

谢谢.

例如

输入字符串:

"
1
2
3
<1...n # of lines content>
4
5
6
"

到所需的输出字符串:

"<1..n # of lines content>"

推荐答案

之前给出的解决方案真的很复杂!所有的需求是

The previously given solutions are really complex! All one needs is

s/^(?:.*\n){1,3}//;
s/(?:.*\n){1,3}\z//;

如果你想接受结尾缺少尾随换行符,你可以使用

If you want to accept a lack of trailing newline at the end, you can use

s/\n?\z/\n/;
s/^(?:.*\n){1,3}//;
s/(?:.*\n){1,3}\z//;

s/^(?:.*\n){1,3}//;
s/(?:.*\n){0,2}.*\n?\z//;

这篇关于Perl/regex 删除字符串的前 3 行和后 3 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆