Hello, Trucide Polère and Claudia,
Trucide, I also found two S/R to achieve what you would like to but, compared to the Claudia’s solution, the second S/R needs to be run, once only :-))) So, just click on the Replace All button, once for each S/R !
My solution works :
Whatever the location of the word “Chapter”, in current line
Whatever the number value, of the word “Chapter” ( Numeric sort not needed )
Whatever the case of the word “Chapter”
If blank line(s) is(are) inserted between two chapters
If blank line(s) is(are) inserted between two lines of a chapter
Remark : These regexes need an extra character, which must NOT exist, yet, in your file. I chose the # symbol but any other symbol may be used. Just escape it if this symbol is a special regex character !
So, given the example text, below, with 3 blank lines after “line 10” :
Chapter 156
line 1
line 2
line 3
line 4
line 5
Chapter 2
line 5
line 6
This is a test : chapter 37
line 7
line 8
line 9
line 10
The first regex S/R, below, adds a line, beginning with a # symbol and followed by the number of the current chapter, just before the line, containing the next word chapter, whatever its case OR before the very end of the file
SEARCH (?i)Chapter\h+(\d+)(?s).+?(?=(?-s).*Chapter|\z)
REPLACE $0#\1\r\n
So it gives the changed text, below :
Chapter 156
line 1
line 2
line 3
line 4
line 5
#156
Chapter 2
line 5
line 6
#2
This is a test : chapter 37
line 7
line 8
line 9
line 10
#37
The second regex S/R, below :
search for a complete line, beginning with a # symbol, and deletes it
search for a non-empty line, which does not contain the word Chapter, whatever its case but it’s followed, further on, by the nearest # symbol with its number, stored as group 1 then adds this number, followed by a dash, in front of each line, during replacement
SEARCH (?i-s)^#.+\R|(?!.*Chapter)^.+(?=(?s).+?#(\d+))
REPLACE ?1\1-$0
So, we obtain the final text, below :
Chapter 156
156-line 1
156- line 2
156-line 3
156- line 4
156-line 5
Chapter 2
2-line 5
2-line 6
This is a test : chapter 37
37-line 7
37- line 8
37- line 9
37-line 10
Best Regards,
guy038
Note : as usual :
The modifier (?-s) means that further dot character ( . ) stands for any single standard character, except for any End of Line character
The modifier (?s) means that further dot character ( . ) stands for any single character, included End of Line character
The modifier (?i) means that the regex search is performed in an insensitive way
The modifier (?s) means that the regex search is performed in a sensitive way