s <- "123-abc-123_abc-123_abc_123_abc" gsub("(_.*$)|\\d+", "\\1", s)