fork download
  1. somenames <- c("Abdul", "Abdul-", "Abdul-Ahad", "Abduallah", "T.")
  2. sort.by.length.desc <- function (v) v[order( -nchar(v)) ]
  3. firstnames <- data.frame(word=sort.by.length.desc(somenames), stringsAsFactors = FALSE)
  4.  
  5. regex.escape <- function(string) {
  6. gsub("([][{}()+*^${|\\\\?.])", "\\\\\\1", string)
  7. }
  8. firstnames$word2 <- paste0("(?!\\B\\w)",regex.escape(firstnames$word),"(?!\\B\\w)")
  9.  
  10. comment <- "Some text with Abdul Abdul- Abduallah Ta, My, and He in it"
  11.  
  12. for(i in 1:length(firstnames$word2)){
  13. comment <- gsub(firstnames$word2[i], "Z", comment, perl=TRUE)
  14. }
  15. comment
  16.  
Success #stdin #stdout 0.28s 41284KB
stdin
Standard input is empty
stdout
[1] "Some text with Z Z Z Ta, My, and He in it"