How should I split and retain elements using strsplit?

前端 未结 3 1187
予麋鹿
予麋鹿 2020-12-28 17:05

What a strsplit function in R does is, match and delete a given regular expression to split the rest of the string into vectors.

>strsplit(\"abc123def\",          


        
相关标签:
3条回答
  • 2020-12-28 17:37

    You could use lookaround assertions.

    > test <- "abc123def"
    > strsplit(test, "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)", perl=T)
    [[1]]
    [1] "abc" "123" "def"
    
    0 讨论(0)
  • 2020-12-28 17:38

    Fundamentally, it seems to me that what you want is not to split on [0-9]+ but to split on the transition between [0-9]+ and everything else. In your string, that transition is not pre-existing. To insert it, you could pre-process with gsub and back-referencing:

    test <- "abc123def"
    strsplit( gsub("([0-9]+)","~\\1~",test), "~" )
    
    [[1]]
    [1] "abc" "123" "def"
    
    0 讨论(0)
  • 2020-12-28 17:39

    You can use strapply from gsubfn package.

    test <- "abc123def"
    strapply(X=test,
             pattern="([^[:digit:]]*)(\\d+)(.+)",
             FUN=c,
             simplify=FALSE)
    
    [[1]]
    [1] "abc" "123" "def"
    
    0 讨论(0)
提交回复
热议问题