x <- "They didn't sell the company, and it went bankrupt" gsub("(?:\\bnot|n't|\\G(?!\\A))\\s+\\K(\\w+)\\b", "not_\\1", x, perl=TRUE)
Standard input is empty
[1] "They didn't not_sell not_the not_company, and it went bankrupt"
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!