fork(2) 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. ## Interleaving function
  11. riffle3 <- function(a, b) {
  12. mlab <- min(length(a), length(b))
  13. seqmlab <- seq(length=mlab)
  14. c(rbind(a[seqmlab], b[seqmlab]), a[-seqmlab], b[-seqmlab])
  15. }
  16. pat <- paste(regex.escape(sort.by.length.desc(v)), collapse="|")
  17. res <- riffle3(regmatches(x, gregexpr(pat, x), invert=TRUE)[[1]], regmatches(x, gregexpr(pat, x))[[1]])
  18. res <- res[res != ""]
  19. res
Success #stdin #stdout 0.22s 185856KB
stdin
Standard input is empty
stdout
[1] "abc"  "[["   "+"    "de.f" "["    "-"    "[["   "g"