Python Regex: a robust way to use Regex for mix EOL-Replacement (CR, LF, CRLF) [duplicate]

Solution 1:

Regex to find any LF that is not part of a CRLF:

(?<!\r)\n

Regex to find any CR that is not part of a CRLF:

\r(?!\n)

Thus Regex to find any non-CRLF lineEnding:

((?<!\r)\n|\r(?!\n))

You can simply replace that with \r\n to fix them all to be CRLFs.


This is using the "Negative Lookbehind" functionality:

(?<\!a)b matches a "b" that was not preceded by an "a".

and the "Negative Lookahead" functionality:

a(?\!b) matches an "a" that is not followed by a "b".

Further documentation here: https://www.regular-expressions.info/lookaround.html