In regular expression (regex), do you know what is the difference between \r\n
, \r
, and \n
? In this article, we will understand the difference between \r\n
, \r
, and \n
with a few code examples.
\r
character is used to denote a Carriage Return (CR), in regular expressions. It is a (non-printable) control character used to reset device or cursor's position to beginning of same or current line of text.
\n
character is used by unix like operating systems to denote a Line Feed (LF). This is also known as line ending, new line, end of line (EOL), next line (NEL) or line break. in regular expressions. It is again a (non-printable) control character used to move to the first position of next line.
\r\n
character combination is used by Windows machines to denote CRLF or New Line, in regular expressions. This combination is used to move to the beginning of next line.
The division dates back to the old days of typewriters (and printers), when you had to turn the wheel to alter the paper's position to change the line and move the carriage to begin typing again at the beginning of a new line. Ther term carriage also comes from the device, typewriter.
\v
or vertical tab moves the cursor to the same position of next line. And,
\t
or tab moves the cursor to 4/8 spaces forward.
How to use \r
and \n
?
In regex search and replace, these are both different characters can be used for the same purpose.
We have a file with text in this format. You can see that there are blank lines, which need to be removed.
1
2
3
Now If we use vim editor (or other vim like editor such as neovim) or a unix like operating system to remove those unwanted lines with the search and replace regex command :%s/[exp1]/[exp2]/g
. to replace all blank lines or replace two new lines (\n
) with one. We can see that if we use exp1 = \n\n
, it matches all new lines. So exp2 should be \n
. But the output is not as expected.
1^@2^@3
Let's try to use carriage return (\r
) for the same purpose as this regex :%s/\r\r/\r/g
and the output (again) is not as expected, this time error says there is no carriage return pattern matched in this file.
E486: Pattern not found: \r\r
Special use of CR and LF character with find and replace
The \r
in the pattern matches a carriage return (CR) char (not any line break character). The \r
construct should be used in the replacement pattern to replace with a new line.
So, you can use it to replace two line break characters with one carriage return pattern. For example,
:%s/\n\n/\r/
1
2
3
To replace recursive newline chars with a single newline, use this regex:
:%s/\n\{2,}/\r/
To account for CRLF endings and replace them with a CR character. This pattern matches two or more ({2,}
) sequences of an optional CR char (\r?
) and then a new line, line feed (LF) char.
:%s/\v(\r?\n){2,}/\r/
Conclusion
In conclusion, the difference between \r
, \n
, and \r\n
in regular expressions is that \r
is a carriage return character that moves the cursor to the beginning of the same line, \n
is a line feed character that moves the cursor to the beginning of the next line, and \r\n
is a combination of the two that is used to denote a new line on Windows systems. These characters can be used in search and replace operations in regular expressions to manipulate text, but it is important to understand the specific behavior of each character and how it is interpreted by different systems.