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

Matches two line immediately following the target line

I have a list like following:

tttttttttttyyyyyyyyyyyyyy 28758.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28759.gif: 98.92%
tttttttttttyyyyyyyyyyyyyy 28760.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28761.gif: 98.54%
tttttttttttyyyyyyyyyyyyyy 28762.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28763.gif: 98.85%
tttttttttttyyyyyyyyyyyyyy 28764.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28765.gif: 99.91%
tttttttttttyyyyyyyyyyyyyy 28766.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28767.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28768.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28834.gif: 100.00%
tttttttttttyyyyyyyyyyyyyy 28835.gif: 100.00%

The following regex bookmarks all lines that include a percentage number under 100.00%:

\b\d{2}\.\d{2}\b%

Now I need a regex that bookmarks all lines including a percentage number under 100.00% and two lines after the target line

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

The following regex can bookmark my target lines and just one line after them:

(?-s)(^.+\R?)\b\d{2}\.\d{2}\b%(?:\R^.+)?

I tried following regex but I failed:

(?-s)(^.+\R?)\b\d{2}\.\d{2}\b%(?:\R^.+)?(?:\R^.+)

where is my regex problem?

>Solution :

Because your regex consumes the lines has percentage lower than 100.00%.

unmatched    tttttttttttyyyyyyyyyyyyyy 28758.gif: 100.00%
matched      tttttttttttyyyyyyyyyyyyyy 28759.gif: 98.92%
consumed     tttttttttttyyyyyyyyyyyyyy 28760.gif: 100.00%
consumed     tttttttttttyyyyyyyyyyyyyy 28761.gif: 98.54%
unmatched    tttttttttttyyyyyyyyyyyyyy 28762.gif: 100.00%

Notice the 4th line didn’t match because your regex consumed it due to the "two lines after the target line" rule.

There are two ways to fix it.

1. Put the "two lines after the target line" rule in a positive lookahead

This way the 2 lines after the target line will still be matched and back-referenced but not consumed. But it creates overlapped matches.

(?-s)(^.+\R?)\b\d{2}\.\d{2}\b%(?=((?:\R^.+){2}))

2. Match "two lines after the target line" but excludes lines that below 100.00%

This way it will matches the next 0 to 2 lines, depends on the percentages of these lines.

(?-s)(^.+\R?)\b\d{2}\.\d{2}\b%(?:\R^(?!.*\b\d{2}\.\d{2}\b%).+){0,2}

Depends on your needs, these two should work for most of your cases.

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