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@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@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.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.