Search code examples
regexnotepad++line

Merge lines after specific word till another specific word in Notepad++


I have been trying to combine lines from a specific word to another specific word:

EOD++::N'
GSM
+
38
+
38
+
32
+
1
'
USF+1+ABCDEFGH' 

The output should be:

EOD++::N'
GSM+38+38+32+1'
USF+1+ABCDEFGH'

I tried to merge lines using regex (\+)\n([0-9]{1,2})\n) but no luck. A help is appreciated :)


Solution

  • You could get all consecutive matches after matching GSM at the start of the string using the \G anchor. You keep the values in the capturing group, and match a newline in between so that they are not part of the replacement.

    Find what:

    (?:^GSM\K\R(?=[\r\n+0-9]+\R')|\G)(\+)\R([0-9]{1,2})\R
    

    Replace with:

    $1$2
    

    Regex demo

    enter image description here

    Explanation

    • (?: Non capture group
      • ^GSM\K\R Match GSM, then \K will forget what is currently matched and then match the newline
      • (?=[\r\n+0-9]+\R') Positive lookahead, assert that what follows are the allowed characters, newline '
      • | Or
      • \G Assert the position at the end of the previous match or at the start
    • ) Close group
    • (\+) Capture group 1, match +
    • \R Match the newline after it
    • ([0-9]{1,2}) Capture group 2, match 1-2 digits
    • \R Match the newline after it