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

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"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top