Search code examples
regexnotepad++

How to Bookmark lines before and after a regex matches until specific line?


I'm sorry for this post title but I didn't find better title for this problem.
I have a list like following:

/////////////////////////
Mitnhnhnksmuion
2,687,064
Etyjyjes
1,897,331
Pihjloyd
1,466,137
Edddlnnnnney
1,297,624
Thjtyjkujkes
1,241,307
Fnnhhnac
1,159,710
AfdBhhhghghBA
1,113,062
Elnhhyhjkukjhn
1,023,500
Bggggggel
1,009,075
Letjyjnhhtrh
991,284
Bahtyjtjyjd
849,265
1980Q4
/////////////////////////
Eayes
4,228,223
Elhyjtyjey
1,456,729
1,412,750
Lein
243
184
AA
1,129
672
Elejntyj345hn
002,570
Neerthty34ond
916
78
Biwertetoel
910,353
Qen
874,812
Bs
877,293
Pyd
850,146
1978Q1
/////////////////////////
Mteichrtertson
2,747,969
Eatertglertees
1,885,332
Pirtertd
1,490,156
Elverts
1,295,789
TtrrheBerteaerttles
1,239,194
Fleterteter
1,156,907
ABB
1,117,183
E
1,027,583
Bi
1,010,372
LedZ
987,821
Barb
850,687
1980Q4
/////////////////////////

following regex bookmark some of above list lines:

(?:^|\R)\K\d+(?:,\d+)*\R\d+(?:,\d+)*(?=\R)

lines that bookmark with this regex:

1,456,729
1,412,750
243
184
1,129
672
916
78

but I don't like this! I want to bookmark my regex matches sections.
for example I want to bookmark following section in my list:

/////////////////////////
Eayes
4,228,223
Elhyjtyjey
1,456,729
1,412,750
Lein
243
184
AA
1,129
672
Elejntyj345hn
002,570
Neerthty34ond
916
78
Biwertetoel
910,353
Qen
874,812
Bs
877,293
Pyd
850,146
1978Q1
/////////////////////////

I tried following regex but not working for me:

^/////////////////////////\R((?:(?!^/////////////////////////).)*)\R\d+(?:,\d+)*\R\d+(?:,\d+)*(?=\R)
^(?:(?!^/////////////////////////).)*\R\d+(?:,\d+)*\R\d+(?:,\d+)*(?=\R/////////////////////////)

how to do this by regex in notepad++?
in other words regex must bookmark all lines before my regex matches until last line include ///////////////////////// and all lines after my regex matches until first line include /////////////////////////


Solution

  • If you want to get the matches in between, you can start the match with only forward slashes on a line.

    Then use \K to not include it in the match, and continue matching all lines that do not have at least 2 consecutive lines matching the numbers.

    Then match at least 2 consecutive lines with the numbers and continue matching all lines that do not contains only forward slashes and assert at the end that there is a line containing only forward slashes.

    ^/+\R\K(?:(?!/+$|\d+(?:,\d+)*\R\d+(?:,\d+)*$).*\R)*\d+(?:,\d+)*(?:\R\d+(?:,\d+)*)+(?:\R(?!/+$).*)*(?=\R/+$)
    

    Regex demo

    Or a bit shorter and a bit more permissive variant:

    ^/+\R\K(?:(?!/+$|\d[\d,]*\R\d[\d,]*$).*\R)*\d[\d,]*(?:\R\d[\d,]*)+(?:\R(?!/+$).*)*(?=\R/+$)
    

    Regex demo