I really strangle to simply extract text between brakets, ().
I have a tibble with 1 column where the values are character chain. And I want to just extract/keep the characters between brakets. I have been reading/trying a lot and sometimes it doesn't work as some examples works only on vectors... not very clear...
Basically, my variable contains values like: ' Country name is (FR)' and I want to create another variable that would have the result: 'FR' ....
Any help, please
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 = "(?<=\\().*(?=\\))")
#>  "FR"
Break the pattern in three parts:
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.
first part checks whether it starts with an opening bracket
second part matched any part within opening and closing bracket
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?