Search code examples
notepad++

Add closing parentheses to end of every Nth line in notepad++


I have some text that repeats like this:

macro num 1
send (pta) (x1 var2 var3
save macro 'n'
macro num 2
send (pta) (x1 var3 var5 var7 var8 var10
save macro 'n'
macro num 3
send (pta) (x1 var5

The first and third lines are always the same (just counting up num 1, num 2, etc.), and the 2nd line always starts with 'send (pta) (x1'.

The thing I am hoping to do is put a closing ) around the end of the series of vars that come after 'send (pta) (x1 ...', and the tricky bit is they are all different ending vars (and there are varying numbers of vars) in the text. Ultimately I'd just want it to look like:

macro num 1
send (pta) (x1 var2 var3)
save macro 'n'
macro num 2
send (pta) (x1 var3 var5 var7 var8 var10)
save macro 'n'
macro num 3
send (pta) (x1 var5) 

Solution

    • Ctrl+H
    • Find what: ^send \(pta\) \([^)\r\n]+\K
    • Replace with: \)
    • TICK Match case
    • TICK Wrap around
    • SELECT Regular expression
    • UNTICK . matches newline
    • Replace all

    Explanation:

    ^                       # beginning of line
    send \(pta\) \(         # literally
    [^)\r\n]+               # 1 or more any character that is not ) or linebreak
    \K                      # forget all we have seen until this position
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here