Regex for nth line in a text file

拜拜、爱过 提交于 2019-12-13 04:49:42

问题


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?

Thanks

Edit:

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

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


回答1:


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]* )

Edit: here is the same thing without multi-line mode

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

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



回答2:


Much better just use Regex.Split

Regex.Split(text, "\n", RegexOptions.Multiline)(3)

That will give you the 4th line in a text string.



来源:https://stackoverflow.com/questions/20008886/regex-for-nth-line-in-a-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!