Rcoding
Rcoding

Reputation: 357

how to remove all characters except for those enclosed by parentheses

Suppose you have a text file like this below

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.

What I aim to do is to erase all characters except for those enclosed by parentheses. Please keep in mind that the characters in a pair of parentheses can be varied ranging from English to other characters but no other punctuations may play a role as enclosing characters: only parentheses can be allowed to do.

I think I should utilize gsub but not sure.

This is what I want to have as a result.

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

Whether using a way of removing or extracting, I hope to get the result above.

Upvotes: 0

Views: 272

Answers (1)

akrun
akrun

Reputation: 886948

We can do this by extracting the substring within the brackets and paste it together

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 )"

Or we can use a gsub based solution

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 )"

data

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."

Upvotes: 1

Related Questions