fork download
  1. regex.escape <- function(string) {
  2. gsub("([][{}()+*^$|\\\\?.])", "\\\\\\1", string)
  3. }
  4. examples <- 'Text: cm+km, +km and C++,Delphi,C++CLI and C++/CLI.'
  5. words <- c('+km', 'C++')
  6. # Unambiguous word boundaries
  7. regex <- paste0('(?<!\\w)(', paste(regex.escape(words), collapse='|'), ')(?!\\w)')
  8. gsub(regex, "<<\\1>>", examples, perl=TRUE)
  9. # => [1] "Text: cm+km, <<+km>> and <<C++>>,Delphi,C++CLI and <<C++>>/CLI."
  10. # Whitespace boundaries
  11. regex <- paste0('(?<!\\S)(', paste(regex.escape(words), collapse='|'), ')(?!\\S)')
  12. gsub(regex, "<<\\1>>", examples, perl=TRUE)
  13. # => [1] "Text: cm+km, <<+km>> and C++,Delphi,C++CLI and C++/CLI."
Success #stdin #stdout 0.21s 39352KB
stdin
Standard input is empty
stdout
[1] "Text: cm+km, <<+km>> and <<C++>>,Delphi,C++CLI and <<C++>>/CLI."
[1] "Text: cm+km, <<+km>> and C++,Delphi,C++CLI and C++/CLI."