0
$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 {}
 \end{equation}}'

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 })

Learning
  • 848
  • 1
  • 9
  • 32

3 Answers3

1

You can use a simple regex like this:

\{([\s\S]*)\}

Regular expression visualization

Working demo

enter image description here

Match information

MATCH 1
1.  [21-226]    `\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}`

As you can see in the match information, the captured content is what you need.

The idea behind this regex is

\{([\s\S]*)\}
      ^--- Capture everything in a greedy way from the first `{` to the last `}`

But also you can do the same thing if you use the s flag (single line):

(?s)\{(.*)\} --> using inline `s` flag
    \{(.*)\} --> using external `s` flag

For PHP code you can have:

$re = "@\\{(.*)\\}@s"; 
$str = "\$str='\add[sometext]{\begin{equation}\label{eqn:3}\nf_{1} =\n\begin{cases}}\n\beta_{1} + \beta_{2}f_{2} & f_{2}\leq \gamma\\\n\beta_{1} + \beta_{2}\gamma + \beta_{4}(f_{2}-\gamma) & f_{2} >\gamma\n\end{cases}sdsdssd,\n\end{equation}}'"; 

preg_match($re, $str, $matches);

Update:

You can use this regex for your updated comments in the question:

\{([\s\S]*equation\})\}

Working demo

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
  • Thanks for the answer. It doesn't solve the problem. If string is `\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}} and some extra code here}} or }` it captures till the last curly braces. I need till `\add tag starting { to ending }` – Learning Mar 06 '15 at 14:06
  • @Vidhya if you look at the screenshot, the green highlight you can see that it captures your expected output – Federico Piazza Mar 06 '15 at 14:11
  • I have updated the post. The string might be having many curly braces. So that I need to get only `\add tag related curly braces` – Learning Mar 06 '15 at 14:17
  • @Vidhya use this regex instead **[working demo](https://regex101.com/r/vT6qQ3/4)** – Federico Piazza Mar 06 '15 at 14:46
  • Actually for my post this would be correct solution. My aim is to get string between related curly braces why because `equation}` may varies. it might be `align}` as such so I can't to specify `equation` string and if I have another `equation}` in my string it will capture that too. So that Can you tell me how to get starting curly braces of `\add[]`tag and ending curly braces only then these limitations can be avoided – Learning Mar 07 '15 at 05:50
1

how about this:

$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}}';

$str= preg_match('/\\\\add\s*\[\s*\w*\]\s*{(.*?)}$/s',$str,$match);

var_dump($match[1]);
benny
  • 315
  • 1
  • 7
0

I have got a regex for my requirement.

$str = preg_replace('/\\\\add\s*\[.*]\s*{(.*?)\\\\end{(.[^\s]*?)}}/s', "$1\\end{\$2}", $str);

Working demo

Learning
  • 848
  • 1
  • 9
  • 32