Find the nine state names with one or more occurrences of “in” followed by a letter. For those states, select two different ways to replace the “in” with “xx” followed by the original following letter. Use Groups and then use a Look Around. state.name[stringr::str_detect(state.name, "in.")] |> stringr::str_replace_all("in[:alpha:]", "xx\\1") stringr::str_subset(state.name, "in.") |> stringr::str_replace_all("in(.)", "xx\\1") stringr::str_subset(state.name, "in.") |> stringr::str_replace_all("in(?=.)", "xx")