according to the documentation, this is the gsub
function, which returns the input string; if there are no matches with the specified match patterns, it returns the entire string.
here, I first use the grepl
function to return the logical vector of the presence / absence of a template in a given line:
ifelse(grepl(".*(Ref. (\\d+)).*", data), gsub(".*(Ref. (\\d+)).*", "\\1", data), "")
embedding this in a function:
mygsub <- function(x){ ans <- ifelse(grepl(".*(Ref. (\\d+)).*", x), gsub(".*(Ref. (\\d+)).*", "\\1", x), "") return(ans) } mygsub(data)
source share