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
gsub
Another simple way is to use gsub but specify you want '' in a new function
noFalsePositives <- function(a,b,x) { return(ifelse(gsub(a,b,x)==x,'',gsub(a,b,x))) } # usage noFalsePositives(".*(Ref. (\\d+)).*", "\\1", data) # [1] "Ref. 12" ""