x <- c("abc", "abc/def","abc/de","abc/d","abc/def/i j k","abc/def/i","abc/def/i j")sub("^(abc)(?:/[^/]*)?", "\\1/def", x)
Standard input is empty
[1] "abc/def" "abc/def" "abc/def" "abc/def" [5] "abc/def/i j k" "abc/def/i" "abc/def/i j"
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!