fork download
  1. x <- "abc[[+de.f[-[[g"
  2. v <- c("+", "-", "[", "[[")
  3.  
  4. ## Escaping function
  5. regex.escape <- function(string) {
  6. gsub("([][{}()+*^$|\\\\?])", "\\\\\\1", string)
  7. }
  8. ## Sorting by length in the descending order function
  9. sort.by.length.desc <- function (v) v[order( -nchar(v)) ]
  10.  
  11. pat <- paste(regex.escape(sort.by.length.desc(v)), collapse="|")
  12. pat <- paste0("(?s)", pat, "|(?:(?!", pat, ").)+")
  13. pat
  14. res <- regmatches(x, gregexpr(pat, x, perl=TRUE))
  15. res
Success #stdin #stdout 0.19s 185728KB
stdin
Standard input is empty
stdout
[1] "(?s)\\[\\[|\\+|-|\\[|(?:(?!\\[\\[|\\+|-|\\[).)+"
[[1]]
[1] "abc"  "[["   "+"    "de.f" "["    "-"    "[["   "g"