Suppose a vector:
xx.1 <- c("zz_ZZ_uu_d", "II_OO_d")
I want to get a new vector splitted from right most and only split once. The expected results would be:
c("zz_ZZ_uu", "d", "II_OO", "d").
It would be like python's rsplit()
function. My current idea is to reverse the string, and split the with str_split()
in stringr
.
Any better solutions?
update
Here is my solution returning n splits, depending on stringr and stringi. It would be nice that someone provides a version with base functions.
rsplit <- function (x, s, n) {
cc1 <- unlist(stringr::str_split(stringi::stri_reverse(x), s, n))
cc2 <- rev(purrr::map_chr(cc1, stringi::stri_reverse))
return(cc2)
}
Negative lookahead:
Where
a(?!b)
says to find such ana
which is not followed by ab
. In this case.*_
means that no matter how far (.*
) there should not be any more_
's.However, it seems to be not that easy to generalise this idea. First, note that it can be rewritten as positive lookahead with
_(?=[^_]*$)
(find_
followed by anything but_
, here$
signifies the end of a string). Then a not very elegant generalisation would bewhere e.g. in case
n=3
this function uses_(?=[^_]*_[^_]*_[^_]*$)
.