I'm sure this is a duplicate, but I can't find it.
The closest I've found so far is:
Extract info inside all parenthesis in R
I am wanting to extract from a string any match that starts with a [, ends with a ], and has a + plus between those brackets. The regular expression I've put together based on a previous answer is a little too greedy.
library(magrittr)
str <- "[a] , [a + b] + [b] , [b - q] , [d - e + f]"
gregexpr(pattern = "(?=\\[).*?[+].*?(?<=\\])",
text = str,
perl = TRUE) %>%
regmatches(x = str,
m = .)
This returns
[[1]]
[1] "[a] , [a + b]" "[b] , [b - q] , [d - e + f]"
where what I want is
[[1]]
[1] "[a + b]" "[d - e + f]"