RStudio ctrl-f 是否可以搜索换行符、换行符或回车符?例如,通过搜索 \n\n\n 来查找成对的空行

Can RStudio ctrl-f search for newline or line feed or carriage return characters? e.g., find pairs of blank lines by searching for \n\n\n

提问人:numbercruncher 提问时间:9/1/2023 更新时间:9/1/2023 访问量:61

问:

可以使用 RStudio 的 ctrl-f 搜索框(并使用正则表达式复选框)搜索文本,并通过在替换框中键入 \n 将其替换为新行,但不能在搜索框中键入 \n 来查找换行符。例如,如果我想查找我的代码在一行中有两个空行的每个位置,并将该对行替换为一个空行,则在搜索框中键入什么?

如果我正在编辑一个 .R 文件,其中包含如下示例所示的文本,我键入 ctrl-f,在正则表达式框中选中一个复选,然后键入

\n\n\n

在“查找”框中,然后键入

\n\n

在“替换”框中, 它应该能够将任何两个相邻的空行变成一个。

如果我在控制台中尝试类似的东西,它确实在那里工作,不像尝试使用 ctrl-f:

before <- 
"line1 of text before just a single blank line

text line that initially was right before the two blank lines


text line that initially was after the two blank lines
last line of text."

after_desired <- 
"line1 of text before just a single blank line

text line that initially was right before the two blank lines

text line that initially was after the two blank lines
last line of text."

after_via_gsub <- gsub("\n\n\n", "\n\n", before)

# see that it worked
cat("BEFORE:\n", before, "\n\n") 
cat("AFTER: \n", after_via_gsub, "\n\n")
after_desired == after_via_gsub

r 正则表达式 rstudio 换行符 字符

评论

1赞 r2evans 9/1/2023
不是很好(而且我对 RStudio IDE 的了解还不够多,无法对它发表更多意见),但您可以做类似的事情clipr::write_clip(gsub("\n\n\n", "\n\n", paste(clipr::read_clip(), collapse = "\n")))
0赞 CAustin 9/1/2023
也许尝试将换行符复制/粘贴到搜索框中?如果这不起作用,那可能只是程序的限制。
0赞 numbercruncher 9/4/2023
我认为这一定是 Rstudio 的局限性,但很高兴看到他们的文档承认这一点。
0赞 numbercruncher 9/4/2023
也许有人可以编写一个加载项,将此功能添加到 Rstudio - 似乎 rstudioapi 包可以使这样的加载项变得可行。

答: 暂无答案