R 如何删除除括号外的所有字符

R 如何删除除括号外的所有字符,r,R,假设您有一个如下所示的文本文件 I have apples, bananas, ( some pineapples over 4 ), and cherries ( coconuts with happy face :D ) and so on. You may help yourself except for cherries ( they are for my parents sorry ;C ) . I feel like I can run a fruit business. 我的目标

假设您有一个如下所示的文本文件

I have apples, bananas, ( some pineapples over 4 ), and cherries ( coconuts with happy face :D ) and so on. You may help yourself except for cherries ( they are for my parents sorry ;C ) . I feel like I can run a fruit business.
我的目标是删除除括号内的字符外的所有字符。请记住,一对括号中的字符可以从英语到其他字符不等,但其他标点符号不能充当封闭字符:只能使用括号

我想我应该使用
gsub
,但不确定

这就是我想要的结果

( some pineapples over 4 ) ( coconuts with happy face :D ) ( they are for my parents sorry ;C )

无论是使用移除还是提取的方法,我都希望得到上面的结果

我们可以通过提取括号内的子字符串并将其粘贴在一起来实现

library(stringr)
paste(str_extract_all(str1, "\\([^)]*\\)")[[1]], collapse=' ')
#[1] "( some pineapples over 4 ) ( coconuts with happy face :D ) ( they are for my parents sorry ;C )"

或者我们可以使用基于
gsub
的解决方案

trimws(gsub("\\s+\\([^)]*\\)(*SKIP)(*FAIL)|.", "", str1, perl = TRUE))
#[1] "( some pineapples over 4 ) ( coconuts with happy face :D ) ( they are for my parents sorry ;C )"
数据
str1@Sotos这不是正确的副本。如果你检查复制链接,它只是提取字符串可能的复制:@akrun-nah,我想把它留在那里,因为很多“从括号中提取”搜索可能会导致这种情况answer@akrun很好。不知道正则表达式是如何工作的…@Sotos的想法是匹配括号内的字符,它被跳过,只允许其他字符(
)匹配并替换为blanksThx!明亮的
str1 <- "I have apples, bananas, ( some pineapples over 4 ), and cherries ( coconuts with happy face :D ) and so on. You may help yourself except for cherries ( they are for my parents sorry ;C ) . I feel like I can run a fruit business."