R - 正则表达式 - 用一个 \r\n 替换多个 \r\n (CRLF)
R - Regex - Replacing multiple \r\n (CRLF) with one \r\n
我试过 gsub( "[\r\n]+", "\r\n", textDoc ) 但它似乎将 \r 和 \n 单独对待,而不是作为单个字符串?
编辑 -
"This is a line! It ends with a CRLF!\r\n
\r\n
\r\n
There is more stuff down here! I want it directly below the other stuff! Get rid of those two blank lines! Actually, ANYTIME I have blank lines, lets remove them!\r\n"
[\r\n]+
使用字符 class 和 []
。这就是 char classes 的工作方式。
您想要 ()
作为捕获组:(\r\n)+
编辑:
实现这个有一些问题,因为它插入了一个额外的 \r
。
经过多次评论,我猜替换应该只是 \n
,并添加了一个 \r\n
(按预期)。
我不完全确定为什么会发生这种情况,但我认为为了确保跨系统兼容性,\n
映射到 \r\n
,因此 \r\n
映射到 \r\r\n
.
我试过 gsub( "[\r\n]+", "\r\n", textDoc ) 但它似乎将 \r 和 \n 单独对待,而不是作为单个字符串?
编辑 -
"This is a line! It ends with a CRLF!\r\n
\r\n
\r\n
There is more stuff down here! I want it directly below the other stuff! Get rid of those two blank lines! Actually, ANYTIME I have blank lines, lets remove them!\r\n"
[\r\n]+
使用字符 class 和 []
。这就是 char classes 的工作方式。
您想要 ()
作为捕获组:(\r\n)+
编辑:
实现这个有一些问题,因为它插入了一个额外的 \r
。
经过多次评论,我猜替换应该只是 \n
,并添加了一个 \r\n
(按预期)。
我不完全确定为什么会发生这种情况,但我认为为了确保跨系统兼容性,\n
映射到 \r\n
,因此 \r\n
映射到 \r\r\n
.