I\'m using the gsub function in R to return occurrences of my pattern (reference numbers) on a list of text. This works great unless no match is found, in whic
based on @joran 's answer
extract_matches <- function(x,pattern,replacement,replacement_nomatch=""){
x <- gsub(pattern,replacement,x)
x[-grep(pattern,x,value = FALSE)] <- replacement_nomatch
x
}
data <- list("with citation (Ref. 12)", "without reference", "")
extract_matches(data, ".*(Ref. (\\d+)).*", "\\1")