Get Characters Inside Parantheses in String in R (Example Code)
In this tutorial you’ll learn how to get all characters inside parantheses within a character string in R.
Example Data
my_string <- "foooo (ooo) and (bar)" # Character string in R my_string # Display example character string # [1] "foooo (ooo) and (bar)" |
my_string <- "foooo (ooo) and (bar)" # Character string in R my_string # Display example character string # [1] "foooo (ooo) and (bar)"
Example: Apply gsub, regmatches & gregexpr Functions to Get Characters Between Parantheses
gsub("[\(\)]", "", # Extract info inside parantheses regmatches(my_string, gregexpr("\(.*?\)", my_string))[[1]]) # [1] "ooo" "bar" |
gsub("[\(\)]", "", # Extract info inside parantheses regmatches(my_string, gregexpr("\(.*?\)", my_string))[[1]]) # [1] "ooo" "bar"