regex multiple pattern with singular replacement

MikeTP picture MikeTP · Sep 7, 2012 · Viewed 42.1k times · Source

I am trying to replace both "st." and "ste." with "st". Seems like the following should work but it does not:

require("stringr")
county <- c("st. landry", "ste. geneveve", "st. louis")
str_replace_all(county, c("st\\.", "ste\\."), "st")

Answer

GSee picture GSee · Sep 7, 2012

You can use | to mean "or"

> str_replace_all(county, "st\\.|ste\\.", "st")
[1] "st landry"   "st geneveve" "st louis"   

Or in base R

> gsub("st\\.|ste\\.", "st", county)
[1] "st landry"   "st geneveve" "st louis"