1

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?

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
avishai445
  • 33
  • 1
  • 4

2 Answers2

2

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]* )
  • Thanks, the regex you gave works in theory but not in PowerGrep for some reason I edited the question with the note about powergrep thanks for the help anyhow – avishai445 Nov 15 '13 at 21:04
  • @user - I added a non multi-line equivalent, see if that works. I'd have to lookup PowerGrep to know why/if it doesn't work there. –  Nov 15 '13 at 21:29
  • 1
    I've tried that regex without multi line it doesn't work it keeps returning the whole 4 lines as a result instead of only the 4th one – avishai445 Nov 15 '13 at 21:43
  • @user - Then it works! You just have to get capture buffer 1. You are getting capture buffer 0 (the whole match). Is it possible to get a capture buffer in PowerGrep? –  Nov 15 '13 at 21:52
  • What about Action Type: Collect Data, Collect `\1` ? –  Nov 15 '13 at 22:05
  • It shows the 4 lines in yellow and the 4th line right after it in green I think that will do, but do you think there's a way to only leave the green part? btw thanks for the help, I'm new to PowerGrep it never occured to me to search @ collect data :P – avishai445 Nov 16 '13 at 08:02
  • I looked at its screen shot. Yes, tell it to collect `\1`, capture group 1. That basically filters it. And the yellow is the whole match, green is capture group 1 (4th line). –  Nov 16 '13 at 18:40
-1

Much better just use Regex.Split

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

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

user890332
  • 1,315
  • 15
  • 15