Let's say I have a vector of string values that looks something like:
vec_vals<-c("value1", "value2", "value3")
and a text string which at certain points may contain those values, something like:
text_string<-"value1 blah blah blah value2"
I am trying to add a value, let's say x.
in the text_string
in front of any of vec_vals
that appear in the text_string
, so in this case the desired result would be:
text_string_cleaned<-"x.value1 blah blah blah x.value2"
Thanks!
You could use the stringr
package:
library(stringr)
vec_vals <- c("value1", "value2", "value3")
text_string <- "value1 blah blah blah value2"
# Create the pattern "(value1|value2|value3)" which in str_replace_all will detect
# all matches of value1, value2, or value3.
pattern <- paste0("(", paste(vec_vals, collapse = "|"), ")")
# Replace all occurrences of value1, value2, or value3 with .xvalue1, .xvalue2, or.xvalue3
str_replace_all(text_string, pattern = pattern, replacement = "x.\\1")
#output
"x.value1 blah blah blah x.value2"
The purpose of \\1
in a regular expression is to insert the exact text that was matched by the first group of characters enclosed in parentheses in the pattern into the replacement string.