Recursive regex in R for curly braces

南楼画角 提交于 2020-01-05 03:54:04

问题


I have some text string in the following pattern.

x = "sdfwervd \calculus{fff}{\trt{sdfsdf} & \trt{sdfsdf} & \trt{sdfsdf} \\{} sdfsdf & sdfsdf & sefgse3 } aserdd wersdf sewtgdf"
  1. I want to use regex to capture the text "fff" in the string \calculus{fff} and replace it with something else.

  2. Further I want to capture the string between the first { after \calculus{.+} and it's corresponding closing curly brace }.

How to do this with regex in R ?

The following captures everything till last curly brace.

gsub("(\\calculus\\{)(.+)(\\})", "", x)

回答1:


For the second task you can use a recursive approach in combination with regmatches() and gregexpr() in base R:

x <- c("sdfwervd \\calculus{fff}{\\trt{sdfsdf} & \\trt{sdfsdf} & \\trt{sdfsdf} \\{} sdfsdf & sdfsdf & sefgse3 } aserdd wersdf sewtgdf")

pattern <- "\\{(?:[^{}]*|(?R))*\\}"
(result <- regmatches(x, gregexpr(pattern, x, perl = TRUE)))


This yields a list of the found submatches:
[[1]]
[1] "{fff}"                                                                          
[2] "{\\trt{sdfsdf} & \\trt{sdfsdf} & \\trt{sdfsdf} \\{} sdfsdf & sdfsdf & sefgse3 }"

See a demo for the expression on regex101.com.



来源:https://stackoverflow.com/questions/49627083/recursive-regex-in-r-for-curly-braces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!