Possible Duplicate:
Extract info inside all parenthesis in R (regex)
I have a string
df
Peoplesoft(id-1290)
I like to capture characters between the parentesis, for example. I like to get id-1290 from the above example.
I used this:
x <- regexpr("\\((.*)\\)", df)
this is giving me numbers like
[1] 10
Is there an easy way to grab text between parentesis using regex in R?
I prefer to use
gsub()for this:The regex works like this:
(.*)\\1In other words, substitute all text in the string with the back reference
If you want to use
regexprather thangsub, then do this:This returns a value of 11, i.e. the starting position of the found expression. And note the attribute
match.lengththat indicates how many characters were matched.You can extract this with
attr:And then use
substringto extract the characters: