如何在RStudio中查找和替换行尾

如何在RStudio中查找和替换行尾

本文介绍了如何在RStudio中查找和替换行尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RStudio 中工作,我想在 .R 脚本的每个选定的非空行的末尾放置一个特定的字符串.

I am working in RStudio and I would like to place a certain string at the end of every selected non-empty line of a .R script.

我尝试使用查找和替换正则表达式,但我只能选择一行的最后一个字符(使用 .$),这没有帮助,因为我不想替换这个字符.我尝试了 \n\r 等的组合,但这些没有返回结果.

I tried to use find and replace with regex but I am only able to select the last character of a line (using .$) which is not helpful as I do not want to replace this character. I have tried combinations of \n\r etc but these return no results.

我正在寻找一种方法将 .$ 选择的字符携带"到我的替换"字符串中(我认为这是不可能的)或选择新行并表达/在我的替换字符串中定义一个新行.

I am seeking a method either to "carry" the character selected by .$ into my "replace" string (I don't think this is possible) or to select the new line and express/define a new line in my replace string.

我既不是 rstudio 高级用户,也不是正则表达式向导,因此非常感谢任何帮助或线索.

I am neither an rstudio power-user nor a regex wizard so any help or clues are greatly appreciated.

推荐答案

尝试以下替换全部:

查找:

(.)$

替换:

\1your_string

数量 (.) 表示捕获给定行的最后一个字符,然后在替换中作为 \1 可用>.请注意,空行不会匹配,因为它们上面没有字符.

The quantity (.) means to capture the last character on a given line, which is then made available in the replacement as \1. Note that empty lines would not match, since they have no characters on them.

这篇关于如何在RStudio中查找和替换行尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:58