R string_extract 匹配两个字符串

R string_extract matching in between two strings

我正在尝试使用 stringr 包中的 str_extract 来匹配其他两个字符串之间的字符串。

我有字符串

sum(jan)

我想要它 return

jan

我试过了

str_extract('sum(jan)', '(?<=sum\().*(?=\)')

但我收到错误

Error in stri_extract_first_regex(string, pattern, opts_regex = opts(pattern)) : 
  Incorrectly nested parentheses in regexp pattern. (U_REGEX_MISMATCHED_PAREN)

它适用于下面的示例,所以我假设它与我尝试匹配括号的方式有关

> str_extract('rooasdfboo', '(?<=roo).*(?=boo)')
[1] "asdf"

感谢任何帮助, 谢谢

这应该可以解决问题:

(?<=sum\().*(?=\))

Regex101.com

问题是双重转义字符,通过使用其中的两个,您实际上是在转义字符“\”,这会与括号混淆。