0

I have strings I have to replace, a ton of them exactly.

They have regular expressions in it:

to_replace <- c("Prop. D. Absatz Pack (w)", "Preis pro konv. Einh.", "Promopreis pro konv. Einh.")
replacing <- c("prop", "preis", "promo")

x <- c("Prop. D. Absatz Pack (w)", "test", "Promopreis pro konv. Einh.")

for (i in seq_along(to_replace)) {
    gsub(pattern = paste0("\\b", to_replace[i] , "\\b"), paste0("\\b", replacing[i], "\\b"), x)
}

It does not work unfortunately. Is there a way to use ALL of these . or ( as normal characters, not as regular expression? Thank you!

Data Mastery
  • 1,555
  • 4
  • 18
  • 60
  • 1
    What you ask is a regex escaping function. This is a solved issue, but there may be another problem here with `\b` word boundaries. Use `(?<!\w)` for the left and `(?!\w)` for the right word boundary instead, and do not forget to add `perl=TRUE` to `gsub` – Wiktor Stribiżew Feb 24 '20 at 15:48
  • thank you for that link, that quotemeta function did the job :-) – Data Mastery Feb 24 '20 at 15:54

0 Answers0