Search code examples
regextext

Remove Double Line Spaces With A Special Symbol @


I have a translation tool, the translation output must match the same line numbers with original file, otherwise it won't save.

My web translator mess up with line breaks. Now I have managed to seperate all sentences with two line breaks.

Now I need help to replace the double lines into a special @ symbol for further processing.

Sample data:

Translated data line 1

Translated data line 2

Translated data line 3

Translated data line 4

Between each lines, even including the last line (line 4), they all follow with double line spaces (2 line breaks).

Is it possible to turn the above turn into the following?

Translated data line 1@
Translated data line 2@
Translated data line 3@
Translated data line 4@

My automation tool has search and replace function. Before I post the asking for help thread here, I did check this one: Matching double line breaks using Regex

The RegEx code does not seem to work for my purpose. /[\r]?\n[\r]?\n/g

Thanks in advance!


Solution

  • You can use

    (?:\r?\n){2}
    

    The matches need to be replaced with @\n. See the regex demo.

    There is another trick to avoid using \n in the replacement: use a capturing group and then use \1 / $1 in the replacement instead of the line free char

    (\r?\n){2}
    

    See this regex demo.