vec <- c('wk0001 V1b','123780 PRO V326b','ttttt V321b')
sub("V.*b$", "", vec)
# [1] "wk0001 " "123780 PRO " "ttttt "
stringr::str_remove(vec, "V.*b$")
# [1] "wk0001 " "123780 PRO " "ttttt "
This also works with the non-greedy "V.*?b$"
, over to you if that's necessary.
BTW: \\b
is a word-boundary, not the literal b
. (V)
is saving it as a group, that's not necessary (and looks a little confusing). The real culprit is that you included ^
, which means start of string (as you mentioned), which will only match if all strings start with V
, and in "Vsomethingb"
. The current vec
strings start with "w"
, "1"
, and "t"
, none of them start with V
.
If you need a guide for regex, https://stackoverflow.com/a/22944075/3358272 is a good guide of many components (and links to questions/answers about them).