Your question seems a bit different from the code you provide: you want to replace acronyms in text that presumably contains dots that aren't acronyms/abbreviations.
This code extracts and identifies acronyms by searching for repeated capital-dot combinations (which can be manually checked and filtered mid-workflow to make sure it's not picking up anything odd), then replaces them using the mgsub
code from Replace multiple arguments with gsub
text1 <- c("The U.S. and the C.I.A. are acronyms. They should be matched.")
m <- gregexpr("([A-Z]\\.)+", text1)
matches <- regmatches(text1, m)[[1]]
matches_nodot <- sapply(matches, gsub, pattern = "\\.", replacement = "")
mgsub <- function(pattern, replacement, x, ...) {
if (length(pattern)!=length(replacement)) {
stop("pattern and replacement do not have the same length.")
}
result <- x
for (i in 1:length(pattern)) {
result <- gsub(pattern[i], replacement[i], result, ...)
}
result
}
text2 <- mgsub(matches, matches_nodot, text1)
text2
# [1] "The US and the CIA are acronyms. They should be matched."