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(
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"
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"