文本文件中第n行的正则表达式 [英] Regex for nth line in a text file

查看:375
本文介绍了文本文件中第n行的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试减少此正则表达式,并搜索了很多答案 我需要从文本文件中获取第n行(在此示例中为第4行) 我在自动取款机上走了

I've been trying to get this regex down and searched alot for answers What I need is to get the nth (4th in this example) line from a text file I got this down atm

^(?<=([^\n]*\n){3})[^\n]*\n

但是它似乎不起作用(关于在后面查找中需要固定长度的模式) 有什么办法可以克服这个障碍?

But it doesn't seem to work (something about needing fixed length patterns in lookbehind) Is there any way to overcome that obstacle?

如有此问题,谁能提供更正\不同的正则表达式?

Can anyone provide a correction\different regex if needed for this problem?

谢谢

我正在PowerGrep中尝试使用此正则表达式,但是它不起作用

I'm trying this regex in PowerGrep and it just doesn't work

P.S: 除了正则表达式,还有其他方法可以在powergrep中获得第n行吗?

P.S: Is there a way of getting nth line in powergrep other than regex?

推荐答案

可能需要使用捕获缓冲区.
此正则表达式使用MULTI_LINE模式.
捕获缓冲区1包含第4行

Probably need to use a capture buffer.
This regex uses MULTI_LINE mode.
Capture buffer 1 contains the 4th line

 #  (?:^[^\n]*\n){3}([^\n]*)

 (?: ^ [^\n]* \n ){3}
 ( [^\n]* )

这是没有多行模式的同一件事

here is the same thing without multi-line mode

 #  ^(?:[^\n]*\n){3}([^\n]*)

 ^ 
 (?: [^\n]* \n ){3}
 ( [^\n]* )

这篇关于文本文件中第n行的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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