R 正则表达式替换可变数量的句点之后的所有句点,前面是行的 space/start
R regex to replace all periods after variable number of periods preceeded by space/start of line
我有以下字符串。我想替换最多 3 个句点之后的所有句点,这些句点之前是行首或带有字母 i
.
的 space
x <- c(
".. ........ ....... ",
"... ........ ....... ",
". ..... ....... . .. ... .... ",
".. ..... ........... .... "
)
期望输出:
x <- c(
".. ...iiiii ...iiii ",
"... ...iiiii ...iiii ",
". ...ii ...iiii . .. ... ...i ",
".. ...ii ...iiiiiiii ...i "
)
我的错误尝试:
gsub('(?:(?:^|\s))(x)', '\U\1', gsub('\.', 'x', x), perl = TRUE)
这是一种获得所需结果的方法,虽然有点笨拙但很有效。基本上,尝试一次完成它的问题似乎是你不知道替换会有多大,所以你可以通过一次一个字符来解决它...
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 "
由 reprex package (v0.2.0) 创建于 2018-09-26。
试试正则表达式 (?<=\.{3})(\S+?)
这会将 3 个周期后的所有周期替换为 i
。
regex
gsub('(?<=\.{3})(\S+?)', 'i', x, perl = TRUE)
我有以下字符串。我想替换最多 3 个句点之后的所有句点,这些句点之前是行首或带有字母 i
.
x <- c(
".. ........ ....... ",
"... ........ ....... ",
". ..... ....... . .. ... .... ",
".. ..... ........... .... "
)
期望输出:
x <- c(
".. ...iiiii ...iiii ",
"... ...iiiii ...iiii ",
". ...ii ...iiii . .. ... ...i ",
".. ...ii ...iiiiiiii ...i "
)
我的错误尝试:
gsub('(?:(?:^|\s))(x)', '\U\1', gsub('\.', 'x', x), perl = TRUE)
这是一种获得所需结果的方法,虽然有点笨拙但很有效。基本上,尝试一次完成它的问题似乎是你不知道替换会有多大,所以你可以通过一次一个字符来解决它...
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 "
由 reprex package (v0.2.0) 创建于 2018-09-26。
试试正则表达式 (?<=\.{3})(\S+?)
这会将 3 个周期后的所有周期替换为 i
。
regex
gsub('(?<=\.{3})(\S+?)', 'i', x, perl = TRUE)