Post

1 follower Follow
0
Avatar

regex replace

I'm a very happy new user of RStudio.

However, I cannot find out how to use regex replace: regex find works fine, but it appears the "\N" style references in replace don't work. This would be a great addition, and would make RStudio more similar to sub/gsub as in R.

Ian Pylvainen

Please sign in to leave a comment.

7 comments

0
Avatar

Nick,

Thanks and I've filed a bug for us to look into this. Would it be possible to show the exact string you used for your find and replace. Would be helpful so we can test it out.

Josh

Ian Pylvainen 0 votes
0
Avatar

Hi Nick, we do $N style backreferences, maybe we need to switch that to \N. We also do $` (backtick) for prefix and $' for suffix, and $& for the whole match.

Ian Pylvainen 0 votes
0
Avatar

Hi Joe.

I searched the help and googled for the syntax, as I supposed there was a solution, but could not find this information before (admittedly, I did not invest a lot of time).
So thank you for this information.

Still, as users of RStudio are bound to know R-style regular expressions best (at least the ones who are ever going to use regex find and replace), I deem it wise to use the same syntax. As an added advantage, this greatly reduces the investment in documentation: you can then simply state in the help that the syntax is the same as for grep(perl=FALSE), except for the extensions you mention below (which I would also alter to be more similar to R-style). A final advantage is that you can probably simply rip off the code from the R sources :-)

Interesting extensions, but it is not completely clear what may be meant by prefix and suffix. Up to where do these run?

In any case thanks and keep up the very good work with RStudio! I am simply in love with "Ctrl+Shift+F" and hope you will add other tools to manage big source files (like a clickable list of the functions that are defined in it). For people like me, who make lots of smallish functions that combine to a bigger effect, these are lifesavers.

Nick Sabbe

ping: nick.sabbe@ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove

Ian Pylvainen 0 votes
0
Avatar

Hello Josh.

In my case, I had a long SQL statement (don't ask, I'm not responsible for the database structure) with a lot of these:
"All_data_may_2011.D003, All_data_may_2011.D004,", which I wanted changed to All_data_may_2011.D003 AS IPCD003, All_data_may_2011.D004 AS IPCD004,".

So I used as find: "2011.D(.{3})," and I wanted to use as replace "2011.D\1 AS IPCD\1,".

By this time, I've used other tools to do this (in fact, I used R :-) ), but according to Joe's response to my question, I should have used "2011.D$1 AS IPCD$1," as the replace pattern.

Thanks for your time and trouble!

Nick Sabbe

ping: nick.sabbe@ugent.be
link: http://biomath.ugent.be
wink: A1.056, Coupure Links 653, 9000 Gent
ring: 09/264.59.36

-- Do Not Disapprove

Ian Pylvainen 0 votes
0
Avatar

Nick,

Thanks for the additional details, we definitely appreciate it and looks like Joe's got this one under control.

All the best,

Josh

Ian Pylvainen 0 votes