Home > database >  How to make a regex to get a value within a certain row range?
How to make a regex to get a value within a certain row range?

Time:06-09

How to make a regex to select entire lines until it reaches a specific value in the limit of 5 lines down? If the value is more than 6 lines down then I should not select.

The configuration I'm doing is:

Multiline
Singleline
Global

This regex works until it reaches the final value, but I didn't want to select myvalue if it was too far from start, as the limit is 5.

start.*?(myvalue here)

The data I'm using:

line 1
line 2
line 3
start
line 1
line 2
line 3
line 4 
line 5
line 6 myvalue here

In the above case, the expected is not to select, if I had it on line 5 then I could select.

Could you show me how to do it in regex?

CodePudding user response:

You may use this regex for making sure only 0 t0 5 line breaks are there between start and end patterns:

^start(?:.*\R){0,5}.*?myvalue here

RegEx Demo 1

This will show no match because myvalue here appears after 6 lines.

If that end pattern appears after 5 lines then there will be a match as you can see in RegEx Demo 2

RegEx Details:

  • ^start: Match start at a line start
  • (?:.*\R){0,5}: Match 0 or more of any characters followed by a line break. Repeat this non-capturing group 0 to 5 times
  • .*?myvalue here: Match myvalue here after matching 0 or more character on a line
  • Related