Replace Certain Line Breaks with Equivalent of Pressing delete key on Keyboard NotePad++ Regex

Viewed 116

Im using Notepad++ Find and replace and I have regex that looks for [^|]\r which will find the end of the line that starts with 8778.

8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBase 
See Also 15990TT|

I want to basically merge that line with the one below it, so it becomes this:

8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBase See Also 15990TT|

Ive tried the replace being a blank space, but its grabbing the last character on that line (an e in this case) and replacing that with a space, so its making it

8778|44523|0||TENNESSEE|ADMINISTRATION||ROLL 169 BATCH 8|1947-09-22|0|OnBas  
See Also 15990TT|

Is there any way to make it essentially merge the two lines?

3 Answers

The issue is you're using [^|] to match anything that's not a pipe character before the carriage return, which, on replacement, will remove that character (hence why you're losing an e).

If it's imperative that you match only carriage returns that follow non-pipe characters, capture the preceding character ([^|])\r$ and then put it back in the replacement using $1.

You're also missing a \n in your regex, which is why the replacement isn't concatenating the two lines. So your search should be ([^|])\r\n$ and your replace should be $1.

\r only matches a carriage return symbol, to match a line break, you need \R that matches any line break sequence.

To keep a part of a pattern after replacement, capture that part with parentheses, and then use a backreference to that group.

So you may use

([^|\r])\R

Replace with $1. Or with $1 if you need to append a space.

Details

  • ([^|\r]) - Capturing group 1 ($1 is the backreference that refers to the group value from the replacement pattern): any char other than | and CR
  • \R - any line break char sequence, LF, CR or CRLF.

See the regex demo and the Notepad++ demo with settings:

enter image description here

Find

(\r\n)+

For "Replace" - don't put anything in (not even a space)

enter image description here

Related