Regex to match each line in a file, with windows and/or linux line break included, even for missing line break at EOF My requirement is to match each line of a text file, including the line terminator of each, at most excluding the terminator of the last line, to take into account the crippled, non POSIX-compiant files generated on Windows; each line terminator can be either \n or \r\n.
As a consequence, no character in the file should be left unmatched.
The best regex I could come up with is this:
\n|\r\n|[^\r\n]++(\r\n|\n)?

Is this the best I can write, performance-wise?
Please, if you use the ^/$ anchors or similar, comment about that, because their behavior is dependent on whether the engine considers them as multiline by default.
I think this would work:
.*(\r?\n|$)

.* matches anything except line breaks (at least '\n', but some regex engines also treat other characters as line breaks).
(\r?\n|$) matches the line break or the end of the string (in case the last line is missing a line break.