2

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!

stackmike
  • 73
  • 5
  • It depends a lot on the software you are using, whether this is feasible at all or not. Try replacing `(?:\r?\n){2}` with `@\n`, see [this regex demo](https://regex101.com/r/O6OWfQ/1). – Wiktor Stribiżew Jun 29 '21 at 15:14
  • Thank you, it works! Also, If I wish to know, instead of replacing with @ symbol. Is there anyway fetch each line using RegEx based on the pattern that they are separated by double line breaks? – stackmike Jun 29 '21 at 16:14
  • To match them, try `^.*(?=(?:\r?\n){2})`. See [this regex demo](https://regex101.com/r/E3MmEK/3). – Wiktor Stribiżew Jun 29 '21 at 17:12

2 Answers2

1

Your pattern [\r]?\n[\r]?\n works, you just have to use @\n in the replacement.

› See the replacements in this regex demo.

Note that you don't need the square brackets.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thanks for the tips and demo. Find and replace is ok now. How to directly fetch the reversed section? Like this regex [RegEx Demo](https://regex101.com/r/P6FA8N/1) How to let it select all the text separated by two line breaks? [Screenshot](https://i.imgur.com/5h1CLXN.jpg) – stackmike Jun 30 '21 at 00:48
  • I did try this code as @Wiktor Stribiżew suggested, but it also matches blank lines, also match #7 and #8 shall be in one line, it is because it is a paragraph with 1 line break. [RegEx - Match Line](https://regex101.com/r/M2CqL5/1) – stackmike Jun 30 '21 at 00:53
  • @stackmike Two lines cannot be captured into a single group, since you can't remove/skip parts of string within a single match operation. – Wiktor Stribiżew Jun 30 '21 at 06:12