s <- c("????", "Cat", "Blue_", "????","D-1") s[!grepl('^[A-Za-z0-9_-]+$', s)] <- 'ABC' s
Standard input is empty
[1] "ABC" "Cat" "Blue_" "ABC" "D-1"
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!