string split on last comma in R

前端 未结 2 794
独厮守ぢ
独厮守ぢ 2020-12-06 06:10

I\'m not new to R but I am relative new to regular expression.

A similar question can be found in here.

An example is if I use

> strsplit(         


        
相关标签:
2条回答
  • 2020-12-06 06:49

    Here's one approach:

    strsplit("UK, USA, Germany", ",(?=[^,]+$)", perl=TRUE)
    
    ## [[1]]
    ## [1] "UK, USA" " Germany"
    

    You may want:

    strsplit("UK, USA, Germany", ",\\s*(?=[^,]+$)", perl=TRUE)
    
    ## [[1]]
    ## [1] "UK, USA" "Germany"
    

    As it will match if there is no space after the comma:

    strsplit(c("UK, USA, Germany", "UK, USA,Germany"), ",\\s*(?=[^,]+$)", perl=TRUE)
    
    ## [[1]]
    ## [1] "UK, USA" "Germany"
    ## 
    ## [[2]]
    ## [1] "UK, USA" "Germany"
    
    0 讨论(0)
  • 2020-12-06 06:59

    You can use stri_split function from stringi package

    x <- "USA,UK,Poland"
    stri_split_fixed(x,",") # standard split by comma
    [[1]]
    [1] "USA"    "UK"     "Poland"
    
    stri_split_fixed(x,",",n = 2) # set the max number of elements
    [[1]]
    [1] "USA"       "UK,Poland"
    

    Unfortunately there is no parameter to change the starting point for splitting (from begin/end) but we can handle this another way - using stri_reverse

    stri_split_fixed(stri_reverse(x),",",n = 2) #reverse
    [[1]]
    [1] "dnaloP" "KU,ASU"
    
    stri_reverse(stri_split_fixed(stri_reverse(x),",",n = 2)[[1]]) #reverse back
    [1] "Poland" "USA,UK"
    stri_reverse(stri_split_fixed(stri_reverse(x),",",n = 2)[[1]])[2:1] #and again :)
    [1] "USA,UK" "Poland"
    
    0 讨论(0)
提交回复
热议问题