I'm reading a .tex file and replacing according to a pattern for save in another .tex file. My left delimiter is
\ket{
and the right delimiter is
}
The regex \\ket\{(.+)\}
can match
\ket{0}
but with complex lines such as
$\ket{\bfG \bfP^L_{2ex}}$, and the real space, $\ket{\bfP^L_{2ex}}$
it matches the entire text
\bfG \bfP^L_{2ex}}$, and the real space, $\ket{\bfP^L_{2ex}
Modifying the regex to
\\ket{([^{}]*|[^}])*}{1,2}
I can detect the mentioned complex line, but in cases such as
reciprocal lattice, $\ket{\bfG \bfP^L_{2ex}{3}{2}}$, and the real space, $\ket{\bfP^L_{2ex}}$
that doesn't work. How can I solve this? What algorithms/topics/books/tutorial must I read to solve problems like this?