Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
f_{1} =
\beta_{1} + \beta_{2}f_{2} & f_{2}\leq \gamma\\
\beta_{1} + \beta_{2}\gamma + \beta_{4}(f_{2}-\gamma) & f_{2} >\gamma
 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:

    f_{1} =
    \beta_{1} + \beta_{2}f_{2} & f_{2}\leq \gamma\\
    \beta_{1} + \beta_{2}\gamma + \beta_{4}(f_{2}-\gamma) & f_{2} >\gamma

I tried:

I don't know how to get related curly braces (i.e.\add tag start { till end })
Updated 6-Mar-15 20:01pm

1 solution

I have got a regex for my requirement.
$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