Rahul
Rahul

Reputation: 11560

How to insert carriage return in regex replace

I want to insert newline character in regex replace what field.

For example:

Find With: (xyxyxy.*[\r\n]+)(yzyzyz)

Replace with: $1xyxyxy\r\nxyxyxy

also tried replace with: $1xyxyxy$r$nxyxyxy

None of them seems to be working.

Question is what is the mantra to insert carriage return and/or new line via regex replace.

Upvotes: 1

Views: 3147

Answers (1)

Tushar
Tushar

Reputation: 87203

You can capture the newline characters and use that in replacement. By capturing the newline characters, you don't have to worry about differences in different OS representation of newline.

Find:

(xyxyxy.*(\r\n|\r|\n)+)(yzyzyz)

Replace:

$1$2$3

$2: Is the single newline character.

Upvotes: 7

Related Questions