Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. a <- "{1:0987617820}{2:q312132498s7yd09f8sydf987s6df8797yds9f87098}
  2. {3:{112:123123214321}}{4:20:asdasd3214213}"
  3.  
  4. ## output should be
  5. "0987617820" "q312132498s7yd09f8sydf987s6df8797yds9f87098", "{112:123123214321}" "20:asdasd3214213"
  6.  
  7. a = "{1:0987617820}{2:q312132498s7yd09f8sydf987s6df8797yds9f87098}{3:{112:123123214321}}{4:20:asdasd3214213}"
  8.  
  9. foohacky = function(str){
  10. #remove opening bracket
  11. pt1 = gsub('\{+[0-9]:', '@@',str)
  12. #remove a closing bracket that is preceded by any alphanumeric character
  13. pt2 = gsub('([0-9a-zA-Z])(\})', '\1',pt1, perl=TRUE)
  14. #split up and hack together the result
  15. pt3 = strsplit(pt2, "@@")[[1]][-1]
  16. pt3
  17. }
  18.  
  19. > foohacky(a)
  20. [1] "0987617820"
  21. [2] "q312132498s7yd09f8sydf987s6df8797yds9f87098"
  22. [3] "{112:123123214321}"
  23. [4] "20:asdasd3214213"
  24.  
  25. > a = "{1:0987617820}{{3:{112:123123214321}}{4:{20:asdasd3214213}}"
  26. > foohacky(a)
  27. [1] "0987617820" "{112:123123214321}" "{20:asdasd3214213}"
  28.  
  29. a <- "{1:0987617820}{2:q312132498s7yd09f8sydf987s6df8797yds9f87098}
  30. {3:{112:123123214321}}{4:20:asdasd3214213}"
  31.  
  32. # split based on }{ allowing for newlines and spaces
  33. out <- strsplit(a, "\}[[:space:]]*\{")
  34. # Make a single vector
  35. out <- unlist(out)
  36. # Have an excess open bracket in first
  37. out[1] <- substring(out[1], 2)
  38. # Have an excess closing bracket in last
  39. n <- length(out)
  40. out[length(out)] <- substring(out[n], 1, nchar(out[n])-1)
  41. # Remove the number colon at the beginning of the string
  42. answer <- gsub("^[0-9]*\:", "", out)
  43.  
  44. > answer
  45. [1] "0987617820"
  46. [2] "q312132498s7yd09f8sydf987s6df8797yds9f87098"
  47. [3] "{112:123123214321}"
  48. [4] "20:asdasd3214213"
  49.  
  50. regPattern <- gregexpr("(?<=\{[0-9]\:)(\{.*\}|.*?)(?=\})", a, perl=TRUE)
  51. a_parse <- regmatches(a, regPattern)
  52. a <- unlist(a_parse)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement