a <- c("this is a number 9999333333 and i got 12344")gsub("(?:\\G(?!^)|(?<!\\d)\\d{5})\\K\\d", "X", a, perl=TRUE)
Standard input is empty
[1] "this is a number 99993XXXXX and i got 12344"
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!