Home > Net >  Pattern to match only characters within parentheses
Pattern to match only characters within parentheses

Time:06-07

I have looked at lots of posts here on SO with suggestions on REGEX patterns to grab texts from parentheses. However, from what I have looked into I cannot find a solution that works.

For example, I have had a look at the following: R - Regular Expression to Extract Text Between Parentheses That Contain Keyword, Extract text in parentheses in R, regex to pickout some text between parenthesis [duplicate]

In the following order, here were the top answers solutions (with some amendments):

pattern1= '\\([^()]*[^()]*\\)'
pattern2= '(?<=\\()[^()]*(?=\\))'
pattern3= '.*\\((.*)\\).*'
all_patterns = c(pattern1, pattern2, pattern3)

I have used the following:

sapply(all_patterns , function(x)stringr::str_extract('I(data^2)', x))

   \\([^()]*[^()]*\\) (?<=\\()[^()]*(?=\\))        .*\\((.*)\\).* 
           "(data^2)"              "data^2"           "I(data^2)" 

None of these seem to only grab the characters within the brackets, so how can I just grab the characters inside brackets?

Expected output:

data

CodePudding user response:

With str_extract, it would extract all those characters matched in the patterns. Instead, use a regex lookaround to match one or more characters that are not a ^ or the closing bracket ()) ([^\\^\\)] ) that succeeds an opening bracket ((?<=\\() - these are escaped (\\) as they are metacharacters

library(stringr)
str_extract('I(data^2)', '(?<=\\()[^\\^\\)] ')
# [1] "data"

CodePudding user response:

Here is combinations of str_extract and str_remove

library(stringr)

str_extract(str_remove('I(data^2)', '.\\('), '\\w*')

[1] "data"
  • Related