The goal is to split a string into lines, except when preceded by a backslash. Let’s visualise. A string like this:
Hello\
world!
Bye, world.
should be split:
[0] Hello world!
[1] Bye, world!
I’ve come up with this regex (for PHP, preg_split):
/(?<!\\\\)\n/
It splits and takes backslashes into account when splitting, but:
- Backslash and newline characters persist.
- It only works with UNIX-like newlines (
\n).
So, it actually outputs:
[0] Hello\\x10 world!
[1] Bye, world!
How should my regex look like?
So far, to solve the issues, I’ve tried:
/(?<!\\\\)(?:\r\n|\n|\r)/
/(?:(?<!\\\\)\n)/
But none of them worked.
You need to do it in two steps:
The first regex makes sure it only splits when there is an odd number of slashes before the newline, so an “escaped slash” is preserved.
Because of this, you might consider replacing the escaped backslashes with real ones at the end: