Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
regex-获取匹配大括号内的内容_R_Regex - Fatal编程技术网

regex-获取匹配大括号内的内容

regex-获取匹配大括号内的内容,r,regex,R,Regex,我有以下表格的文本: text <- "\\examples{afunction(x = list()){\nx<-3\n}y<-2 server <- function(input, output, session) {\n output$res_bttn1 <- renderPrint({input$bttn1})\n}\n}" 问题是\\examples{}中还有其他大括号。因此,我认为我必须找到一个正则表达式来检测哪个}关闭\\exa

我有以下表格的文本:

text <- "\\examples{afunction(x = list()){\nx<-3\n}y<-2 server <- function(input, output, session) {\n  output$res_bttn1 <- renderPrint({input$bttn1})\n}\n}"
问题是
\\examples{}
中还有其他大括号。因此,我认为我必须找到一个正则表达式来检测哪个
}
关闭
\\examples{
(但我愿意接受关于如何获得相同结果的其他建议)

我怎样才能做到这一点呢?首选基本R解决方案。

您可以使用

\\examples({([^{}]*?(?:(?1)[^{}]*?)*)\s*)

详细信息

  • \\examples
    -一个
    \examples
    字符串
  • ({([^{}]*?(?:(?1)[^{}]*?)*)\s*})
    -组1(这是必需的,因为正则表达式子例程
    (?1)
    将递归此子模式):
    • {
      -a
      {
      字符
    • ([^{}]*?(?:(?1)[^{}]*?)*)
      -第2组(您需要的值):
      • [^{}]*?
        -除
        {
        }
        之外,尽可能少的字符为零或更多
      • (?:(?1)[^{}]*?)*
        -整个第1组模式的零次或多次出现,后跟零次或多次字符,而不是尽可能少的
        {
        }
    • \s*
      -零个或多个空格(在尾随
      }
      之前修剪空格)
    • }
      -a
      }
      字符
见:


text[1]“一个函数(x=list()){\n这个函数行吗?
sub(\\{(.\\\},'\\1',text)
匹配到最后一个
'\n'
?这个例子很好,但我不确定在最后一个
之前是否总是有一个
\n
"afunction(x = list()){\nx<-3\n}y<-2 server <- function(input, output, session) {\n  output$res_bttn1 <- renderPrint({input$bttn1})\n}"