[R] Replace multiple subexpressions at once?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Tue Oct 25 12:28:33 CEST 2022


An R regular expression pattern can contain parenthesized 
subexpressions, and those can be used later in the same pattern, or in 
the replacement in sub() and gsub().  What I'd like to do is match 
several subexpressions and replace all of them.

For example, I might have a string like

     s <- "<div data-pos=\"Untitled.knit.md using 12:1-13:1\">"

and I can use the regular expression

     regexp <- ".*<[^>]+ data-pos=\"([^\"]*)@([[:digit:]]+):.*"

to match the whole thing, with \\1 matching "Untitled.knit.md" and \\2 
matching "12".  I'd like to replace the first with "newname" and the 
second with "newnumber", so the string ends up looking like

     s <- "<div data-pos=\"newname using newnumber:1-13:1\">"

I could write a function to do this using regexec() and two calls to 
`substring<-`(), but I'm hoping someone has already done that.  Any 
suggestions?

Duncan Murdoch

P.S.  I'm going to be travelling for a couple of weeks, and so may not 
respond right away.



More information about the R-help mailing list