Extract text between brakets ()

Note: I deleted my earlier post to have both solution and explanation together.

Here's a solution using regular expressions:

fake_variable <- ' Country name is (FR)'
modified_fake_variable <- stringr::str_extract(string = fake_variable,
                                               pattern = "(?<=\\().*(?=\\))")
modified_fake_variable
#> [1] "FR"

Break the pattern in three parts:

  1. (?<=\\()
  2. .*
  3. (?=\\))

The second part is straightforward. It matches zero or more number of any character in between the opening and closing brackets.

The first part (and the third part) uses positive look behind (and positive look ahead). What they does is that it checks whether the pattern after ?<= (and ?=) and with a pair of parentheses is present or not. If present, it returns the part that matches the pattern given after (and before) this, but not the part that matches itself.

So, basically:

  1. first part checks whether it starts with an opening bracket
  2. second part matched any part within opening and closing bracket
  3. third part checks whether it ends with a closing bracket

I'm not really very experienced with regular expressions, so it can be done more easily and my solution and/or explanation may not be accurate in all possible scenarios. You can check this site.

Hope this helps.


If your question is answered, will you please consider marking this thread as solved?

2 Likes