Woody Chan
Woody Chan

Reputation: 69

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?

Upvotes: 1

Views: 92

Answers (2)

lemon
lemon

Reputation: 15482

You can use the following regex ((BUY|SELL)[^\n]+\n)\s+ and replace with \1\2.

Regex Match Explanation:

  • ((BUY|SELL)[^\n]+\n): Group 1
    • (BUY|SELL): Group 2
      • BUY: sequence of characters "BUY" followed by a space
      • |: or
      • SELL: sequence of characters "SELL" followed by a space
    • [^\n]+: any character other than newline
    • \n: newline character
  • \s+: any space characters

Regex Replace Explanation:

  • \1: Reference to Group 1
  • \2: Reference to Group 2

Check the demo here. Tested on Notepad++ in a private environment too.

Note: Make sure to check the "Regular expression" checkbox.

Regex

Upvotes: 1

Toto
Toto

Reputation: 91415

  • 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

Upvotes: 1

Related Questions