Search code examples
regexnotepad++

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


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?


Solution

  • \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