这里有一个方法来获得想要的结果,这是有点笨重,但工作。基本上看来,尝试一次性完成的问题是,你不知道替代品会有多大,所以你可以通过一次只做一个字符来解决这个问题。。。
x <- c(
".. ........ ....... ",
"... ........ ....... ",
". ..... ....... . .. ... .... ",
".. ..... ........... .... "
)
library(stringr)
dots_to_i <- function(chr){
pat_p <- "(?<=(^| )\\.{3})\\."
pat_i <- "(?<=i)\\."
while (any(str_detect(chr, pat_p)) | any(str_detect(chr, pat_i))){
chr <- chr %>%
str_replace_all(pat_p, "i") %>%
str_replace_all(pat_i, "i")
}
return(chr)
}
dots_to_i(x)
#> [1] ".. ...iiiii ...iiii " "... ...iiiii ...iiii "
#> [3] ". ...ii ...iiii . .. ... ...i " ".. ...ii ...iiiiiiii ...i "
创建日期:2018-09-26
reprex package
(第0.2.0版)。