For example this matches every word of length 3 or more, and replaces it with 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"
What I'd like to get is:
#"Thi is a lon" "Ano one."
You can use the following to match:
([a-zA-Z]{3})[a-zA-Z]+
And replace with \\1
You can also use gsub
(from comments)
gsub("([a-zA-Z]{3})[a-zA-Z]+", "\\1", c("This is a long", "Another one."))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With