Home > front end >  Notepad remove linebreak in between two specific strings
Notepad remove linebreak in between two specific strings

Time:01-19

I have something like this

\text
This is a sentence.
This is another sentence.
\endtext

I want to remove the line break in between the two sentences in all instances of \text and \endtext. In order to look like this

\text
This is a sentence. This is another sentence.
\endtext

But of course where it gets complicated is that there are also line breaks after \text and before \endtext. These I want to keep. So, logically and in english speaking, what I was looking to do is something like

(after "\text\n") (remove all instances of \n) (before "\n\endtext")

but since I'm not very good at regex, I'm not sure how that would be written out in that language. Could someone help?

CodePudding user response:

Notepad supports PCRE.

You can use this regex to search:

(?:\\text\R|(?<!\A)\G).*\K\R(?!\\endtext)

Replace this with a space.

RegEx Demo

RegEx Breakdown:

  • (?:: Start non-capture group
    • \\text: Match \text
    • \R: Match a line break
    • |: OR
    • (?<!\A)\G: Start from the end of the previous match. (?<!\A) is to make sure that we are not the start position
  • ): End non-capture group
  • .*: Match 0 or more any character except line break
  • \K: Reset match info
  • \R: Match a line break
  • (?!\\endtext): Negative lookahead to make sure that we don't have Match \endtext` right next to the current position
  • Related