Text outside the bracket
> x <- c("a(b)jk(p)" ,"ipq" , "e(ijkl)") > gsub("\\([^()]*\\)", "", x) [1] "ajk" "ipq" "e"
The text inside the parenthesis
> x <- c("a(b)jk(p)" ,"ipq" , "e(ijkl)") > gsub("(?<=\\()[^()]*(?=\\))(*SKIP)(*F)|.", "", x, perl=T) [1] "bp" "" "ijkl"
(?<=\\()[^()]*(?=\\)) matches all characters that are present inside the brackets, and then (*SKIP)(*F) cause the match to fail. Now he is trying to execute the template that was immediately after the symbol | against the remaining line. So the point . matches all characters that are not yet missing. Replacing all matching characters with an empty string will give only the text present in the rockets.
> gsub("\\(([^()]*)\\)|.", "\\1", x, perl=T) [1] "bp" "" "ijkl"
This regular expression will capture all characters that are in brackets, and matches all other characters. |. or a part helps to match all other characters other than captured ones. Therefore, replacing all the characters with the characters present within the group, index 1 will give you the desired result.
source share