Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
PHP
$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:
PHP
$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 })
Posted
Updated 6-Mar-15 20:01pm
v2

1 solution

I have got a regex for my requirement.
PHP
$str = preg_replace('/\\\\add\s*\[.*]\s*{(.*?)\\\\end{(.[^\s]*?)}}/s', "$1\\end{\$2}", $str);


working demo
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900