在R中删除空格前的换行符。

问题描述 投票:0回答:1

我想在R中删除下一行以空格或制表符开头的换行符。

text <- gsub("\n(?=\\h)","",x,perl=True)

但没有成功

r regex newline
1个回答
1
投票

您可以使用

text <- gsub("\\R(?=\\h)", "", x, perl=TRUE)

\R(?=\h) 模式匹配任何换行序列(用 \R)之前的任何水平空白处(用 (?=\h) 正面看)。)

见一个 R演示:

x <- "Line 1\r\n coninuation of line 1\r\nLine 2"
gsub("\\R(?=\\h)", "", x, perl=TRUE)
## => [1] "Line 1 coninuation of line 1\r\nLine 2"

0
投票

我们可以使用 TRUE 而不是 TrueR

gsub("\n(?=\\h)","",x,perl=TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.