gsub to return all matches of an expression instead of just the last match
r, regex
Solution
You can use the `strapply` function from the `gsubfn` package to do this:
library(gsubfn)
data <- list("a sentence with citation (Ref. 12) and another (Ref. 13)", "single (Ref. 14)")
unlist(strapply(data,"(Ref. (\\d+))"))
Problem
I'm looking for a gsub string that will return all matches of an expression, rather than just the last match. i.e.: ``` data <- list("a sentence with citation (Ref. 12) and another (Ref. 13)", "single (Ref. 14)") gsub(".*(Ref. (\\d+)).*", "\\1", data) ``` Returns ``` [1] "Ref. 13" "Ref. 14" ``` so I've lost Ref. 12.