Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find and replace end of a line in RStudio

Tags:

regex

rstudio

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.

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.

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

edit: Apologies this may be a moot question. There is an April 2016 mention here that says the ability to find/replace line endings has not been implemented. Is there a way to check on progress of a feature request?

like image 841
bmrn Avatar asked Aug 17 '17 01:08

bmrn


People also ask

How do I find and replace in R studio?

Find and Replace RStudio supports finding and replacing text within source documents: Find and replace can be opened using the Ctrl+F shortcut key, or from the Edit -> Find... menu item.

How do you break a line in R studio?

To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .

How do you replace text in R studio?

If you're not familiar with this feature, try it out: press Ctrl+Shift+F (MacOS: Cmd+Shift+F), or choose Find in Files… from the Edit menu. After you've done a search, switch to Replace view via the toggle, enter your new text, and click Replace All. It works with regular expressions, too.


1 Answers

Try the following replace all:

Find:

(.)$

Replace:

\1your_string

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.

like image 133
Tim Biegeleisen Avatar answered Sep 24 '22 12:09

Tim Biegeleisen