new<-c("111", "1234567891", "12")sub("^(\\d{3})\\d{7}$", "\\1", new)
Standard input is empty
[1] "111" "123" "12"
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!