Regex group capture in R with multiple capture-groups
str_match()
, from the stringr
package, will do this. It returns a character matrix with one column for each group in the match (and one for the whole match):
> s = c("(sometext :: 0.1231313213)", "(moretext :: 0.111222)")> str_match(s, "\\((.*?) :: (0\\.[0-9]+)\\)") [,1] [,2] [,3] [1,] "(sometext :: 0.1231313213)" "sometext" "0.1231313213"[2,] "(moretext :: 0.111222)" "moretext" "0.111222"
gsub does this, from your example:
gsub("\\((.*?) :: (0\\.[0-9]+)\\)","\\1 \\2", "(sometext :: 0.1231313213)")[1] "sometext 0.1231313213"
you need to double escape the \s in the quotes then they work for the regex.
Hope this helps.
Try regmatches()
and regexec()
:
regmatches("(sometext :: 0.1231313213)",regexec("\\((.*?) :: (0\\.[0-9]+)\\)","(sometext :: 0.1231313213)"))[[1]][1] "(sometext :: 0.1231313213)" "sometext" "0.1231313213"