提问人:numbercruncher 提问时间:9/1/2023 更新时间:9/1/2023 访问量:61
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
问:
可以使用 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
答: 暂无答案
评论
clipr::write_clip(gsub("\n\n\n", "\n\n", paste(clipr::read_clip(), collapse = "\n")))