Search code examples
regexnotepad++

Regex to disregard partial matches across lines / matching too much


I have three lines of tab-separated values:

  1. SELL 2022-06-28 12:42:27 39.42 0.29 11.43180000 0.00003582
  2. BUY 2022-06-28 12:27:22 39.30 0.10 3.93000000 0.00001233
  3. _____2022-06-28 12:27:22 39.30 0.19 7.46700000 0.00002342

The first two have 'SELL' or 'BUY' as first value but the third one has not, hence a Tab mark where I wrote ______:

enter image description here

I would like to capture the following using Regex:

enter image description here

My expression ^(BUY|SELL).+?\r\n\t does not work as it gets me this:

enter image description here

I do know why outputs this - adding an lazy-maker '?' obviously won't help. I don't get lookarounds to work either, if they are the right means at all. I need something like 'Match \r\n\t only or \r\n(?:^\t) at the end of each line'.

The final goal is to make the three lines look at this at the end, so I will need to replace the match with capturing groups:

enter image description here

Can anyone point me to the right direction?


Solution

    • Ctrl+H
    • Find what: ^(BUY|SELL).+\R\K\t
    • Replace with: $1\t
    • CHECK Match case
    • CHECK Wrap around
    • CHECK Regular expression
    • UNCHECK . matches newline
    • Replace all

    Explanation:

    ^               # beginning of line
    (BUY|SELL)      # group 1, BUY or SELL
    .+              # 1 or more any character but newline
    \R              # any kind of linebreak
    \K              # forget all we have seen until this position
    \t              # a tabulation
    

    Replacement:

    $1              # content of group 1
    \t              # a tabulation
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here