Replacing all umlauts simultaneously in R (using regex)

r, regex

Solution

You could try

# install.packages("stringi) # uncomment & run if needed
str <- c("äöü", "ÄÖÜ")
stringi::stri_replace_all_fixed(
  str, 
  c("ä", "ö", "ü", "Ä", "Ö", "Ü"), 
  c("ae", "oe", "ue", "Ae", "Oe", "Ue"), 
  vectorize_all = FALSE
)
# [1] "aeoeue" "AeOeUe"

Problem

I have text in German and I want to replace all umlauts (ä, Ä, ü, Ü, ö, Ö) with ae, oe, ue, etc. I can do it separately (by saving each substitution into a new file): ``` gsub(pattern = '[ä]', replacement = "ae",text) gsub(pattern = '[ü]', replacement = "ue",text) gsub(pattern = '[ö]', replacement = "oe",text) ``` But can I do it in one command (including substituting capital letters with Ae, Oe and Ue, etc.)? Can I do it by regex?

Original source