Reputation: 109984
I have the following strings. I want to replace all periods that come after up to 3 periods the were preceded by a begining of line or a space with a letter i
.
x <- c(
".. ........ ....... ",
"... ........ ....... ",
". ..... ....... . .. ... .... ",
".. ..... ........... .... "
)
Desired utput:
x <- c(
".. ...iiiii ...iiii ",
"... ...iiiii ...iiii ",
". ...ii ...iiii . .. ... ...i ",
".. ...ii ...iiiiiiii ...i "
)
My very wrong attempt:
gsub('(?:(?:^|\\s))(x)', '\\U\\1', gsub('\\.', 'x', x), perl = TRUE)
Upvotes: 1
Views: 75
Reputation: 1091
Try regex (?<=\.{3})(\S+?)
This will replace all the periods after 3 periods to i
.
regex
gsub('(?<=\\.{3})(\\S+?)', 'i', x, perl = TRUE)
Upvotes: 4
Reputation: 15072
Here's a way of getting the desired result that is a little clunky but works. It basically seems like the problem with trying to do it in one go is that you don't know how big the replacement is going to be, so you can work around it by doing it one character at a time...
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 "
Created on 2018-09-26 by the reprex package (v0.2.0).
Upvotes: 1