Is it possible to use stored variables in R’s regex?
For example I want to remove quotes around decimal numbers in the following string s = "\"Bob\",\"1\",\"Mary\",\"2\"" – in most languages you could do something like sub("\"(\d)\"","$1",s) but I cannot seem to find the capability in R. Any help would be greatly appreciated.
Also as a side question does R have the \d support? (it throws an error when i try it) Thanks
I believe this is usually called back referencing. In R, you can use \\1 \\2, etc.
I’m not sure about \d support in R. I generally just use [0-9] anyway, since I know it works and I find it easier to read.
Edit: @Andrie and @Richie Cotton both offered two suggestions in comments, which I will include here for completeness. [:digits:] works, but to my mind offers little in readability over [0-9]. \\d works as well.