Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading