R how to match regex "_a (b)"

67 Views Asked by At

I am crazy about this question. In R language regex, how to match a pattern "_a (b)"? a and b both stand for a word, there is a space in front of (.

library(stringr)
x <- c("dum_drop (words)", "apple")
# I want to match and remove the part "_drop (words)"
str_remove(x, pattern = "[_drop (words)]")
# result
# [1] "um_drop (words)" "aple" 

I think the regex expression about the pattern "_drop (words)" needs some work.

1

There are 1 best solutions below

0
Beryl On

Correct match:

x <- c("dum_drop (words)", "apple")
str_remove(x, pattern = "_drop [(]words[)]")
# result
# [1] "dum"   "apple"