例如,这匹配长度为3或更长的每个单词,并将其替换为xx
:
library(stringr)
str_replace_all(c("This is a long", "Another one."), "([a-zA-Z]{3,})", "xx")
#output: "xx is a xx" "xx xx"
我想得到的是:
#"Thi is a lon" "Ano one."
答案 0 :(得分:4)
您可以使用以下内容进行匹配:
([a-zA-Z]{3})[a-zA-Z]+
并替换为\\1
您也可以使用gsub
(来自评论)
gsub("([a-zA-Z]{3})[a-zA-Z]+", "\\1", c("This is a long", "Another one."))