regex R如何匹配正则表达式“_a(B)”

z31licg0  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(121)

我对这个问题很着迷。在R语言正则表达式中,如何匹配一个模式“_a(b)"?a和b都代表一个单词,(前面有一个空格。

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"

我认为关于模式“_drop(words)”的正则表达式需要做一些工作。

2vuwiymt

2vuwiymt1#

正确匹配:

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

相关问题