如何捕获相关花括号之间的字符串 [英] How to capture string between related curly braces
问题描述
$ str = ' \ add [sometext] {\ begin { equation} \ label {eqn:3}
f_ {1} =
\ begin {cases}}
\ betaa_ {1} + \beta_ {2} f_ {2 }& f_ {2} \ leq \\\\\\\\\\\\\\\\\\\\\\\\\\ )& f_ {2}> \ gamma
\ end {cases} sdsdssd,
\ end {equation}}
这里可能有一些额外的代码{dfdfd}
\ end {方程}}和一些有大量花括号的文字}}'
我需要在\ addd [sometext]之间提取字符串] {和}(ietill \ add标签结束花括号)\ add [sometext] {和}之间的字符串可能会有所不同,所以我不能在正则表达式模式中指定这些字符串我应该只考虑开始和结束花括号\ add [sometext]
预期输出:
\ begin {equation} \ label {eqn:3}
f_ {1} =
\ begin {cases}
\beta_ {1} + \beta_ {2} f_ {2}& f_ {2} \ leq \\\\\\\\\\\\\\\\\\\\\\\\\\ )& f_ {2}> \ gamma
\ end {cases} sdsdssd,
\ end {equation}
我试过:
$ str = preg_replace(' / \\ \\add\s * \ [\s * \w * \] \s * {(。*?)} /秒,$ 1,$匹配)跨度>
我不知道如何获得相关的花括号(ie\d tag start {until end})
str = ' \ add [sometext] {\begin {equation} \ label {eqn:3}
f_ {1} =
\ begin {cases}}
\beta_ {1} + \beta_ {2} f_ {2}& f_ {2} \ leq \\\\\\\\\\\\\\\\\\\\\\\\\\ )& f_ {2}> \ gamma
\ end {cases} sdsdssd,
\ end {equation}}
这里可能有一些额外的代码{dfdfd}
\ end {方程}}和一些有大量花括号的文字}}'
我需要在\ addd [sometext]之间提取字符串] {和}(ietill \ add标签结束花括号)\ add [sometext] {和}之间的字符串可能会有所不同,所以我不能在正则表达式模式中指定这些字符串我应该只考虑开始和结束花括号\ add [sometext]
预期输出:
\ begin {equation} \ label {eqn:3}
f_ {1} =
\ begin {cases}
\beta_ {1} + \beta_ {2} f_ {2}& f_ {2} \ leq \\\\\\\\\\\\\\\\\\\\\\\\\\ )& f_ {2}> \ gamma
\ end {cases} sdsdssd,
\ end {equation}
我试过:
str = preg_replace(' / * \\\\add\s \ [\s * \w * \] \s * {(。*?)} / S,
1,
$str='\add[sometext]{\begin{equation}\label{eqn:3}
f_{1} =
\begin{cases}}
\beta_{1} + \beta_{2}f_{2} & f_{2}\leq \gamma\\
\beta_{1} + \beta_{2}\gamma + \beta_{4}(f_{2}-\gamma) & f_{2} >\gamma
\end{cases}sdsdssd,
\end{equation}}
it may have some extra code here with {dfdfd}
\end{equation}}and some text with so many curly braces }}'
I need to extract the string between \add[sometext]{ and }(i.e.till \add tag end curly braces)The string between \add[sometext]{ and } may varies so I can't specify these string in regex pattern I should only consider starting and ending curly braces of \add[sometext]
Expected output:
\begin{equation}\label{eqn:3}
f_{1} =
\begin{cases}
\beta_{1} + \beta_{2}f_{2} & f_{2}\leq \gamma\\
\beta_{1} + \beta_{2}\gamma + \beta_{4}(f_{2}-\gamma) & f_{2} >\gamma
\end{cases}sdsdssd,
\end{equation}
I tried:
$str=preg_replace('/\\\\add\s*\[\s*\w*\]\s*{(.*?)}/s,$1,$match)
I don't know how to get related curly braces (i.e.\add tag start { till end })
str='\add[sometext]{\begin{equation}\label{eqn:3} f_{1} = \begin{cases}} \beta_{1} + \beta_{2}f_{2} & f_{2}\leq \gamma\\ \beta_{1} + \beta_{2}\gamma + \beta_{4}(f_{2}-\gamma) & f_{2} >\gamma \end{cases}sdsdssd, \end{equation}} it may have some extra code here with {dfdfd} \end{equation}}and some text with so many curly braces }}'
I need to extract the string between \add[sometext]{ and }(i.e.till \add tag end curly braces)The string between \add[sometext]{ and } may varies so I can't specify these string in regex pattern I should only consider starting and ending curly braces of \add[sometext]
Expected output:
\begin{equation}\label{eqn:3} f_{1} = \begin{cases} \beta_{1} + \beta_{2}f_{2} & f_{2}\leq \gamma\\ \beta_{1} + \beta_{2}\gamma + \beta_{4}(f_{2}-\gamma) & f_{2} >\gamma \end{cases}sdsdssd, \end{equation}
I tried:
str=preg_replace('/\\\\add\s*\[\s*\w*\]\s*{(.*?)}/s,
1,
这篇关于如何捕获相关花括号之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!