fork download
  1. myList <- c("at","ax","CL","OZ","Gm","Kg","C100","-1.00")
  2. myText <- "This is at Sample ax Text, which CL is OZ better and cleaned Gm, where C100 is not equal to -1.00. This is messy text Kg."
  3. escape <- function(s) { return(gsub("([{[()|?$^*+.\\])", "\\\\\\1", s)) }
  4. pat <- paste0("\\s*(?<!\\w)(?:", paste(sapply(myList, function(t) escape(t)), collapse = "|"), ")(?!\\w)")
  5. cat(pat, collapse="\n")
  6. gsub(pat, "", myText, perl=TRUE)
  7.  
  8.  
Success #stdin #stdout 0.23s 39600KB
stdin
Standard input is empty
stdout
\s*(?<!\w)(?:at|ax|CL|OZ|Gm|Kg|C100|-1\.00)(?!\w) 
[1] "This is Sample Text, which is better and cleaned, where is not equal to. This is messy text."