c0bra
c0bra

Reputation: 1090

recursive regex for matching bracket pairs in gnu r

I am experimenting with a recursive regex in R and the stringr package. Somehow it gives me an syntax error: U_REGEX_RULE_SYNTAX

The regex is working correctly and matches only the matching brackets:

https://regex101.com/r/Uv9Xy4/1

But in R it gives me said syntax error:

str_extract("((blub))(", "(?s)\\((?:[^()]+|(?R))*+\\)")

Am I missing the escape of any control character?

Upvotes: 1

Views: 174

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

ICU regex library used in stringr is not capable of everything PCRE can do. ICU regex engine does not support recursion.

So, use base R with perl=TRUE:

x <- "((blub))("
regmatches(x, regexpr("\\((?:[^()]+|(?R))*+\\)", x, perl=TRUE))
## => [1] "((blub))"

Note that (?s) DOTALL modifier is redundant here since there is no . in the pattern and can safely be removed.

Upvotes: 2

Related Questions