I have the following text
\begin{comment}
TEXT
\end{comment}
\begin{verbatim}
TEXT
\end{verbatim}
and the regular expression "\\begin{\([^}]*\)}
to match the \begin
statements and capture the comment
or verbatim
words. Now, I would like to match the \end
statements. Given I already had a match for \begin
I thought I could reference the previous match and look for \\end{\\1}
, however I am getting Invalid back reference
.
Am I doing something wrong? Is there a way to reference previous matches in a regular expression?
"\\\\begin{\\([^}]*\\)}\n.*?\n\\\\end{\\1}"
.
does not match newlines, the.*?
should be\\(.\\|\n\\)*?
if you needed it to match with arbitrary multi-line TEXT.