Regex to match the first occurrence before a word

0

My string is as follows:

\questao{1}

\begin{enumerate}
todos os tipos de caracteres
\end{enumerate}

\begin{enumerate}       -------> APENAS este deve dar match
todos os tipos de caracteres
\end{enumerate}

\questao{2}

I'm trying to make a regex that can identify ONLY the first occurrence of \begin{enumerate} BEFORE each \questao .

I broke my head, but the most I got was the match in the first \begin{enumerate} when there was nothing between it and \questao . But in the case I need, it will have some lines filled below \ begin {enumerate}.

    
asked by anonymous 30.03.2018 / 15:49

1 answer

0

Try this regex to catch only the last occurrence of \begin before every occurrence of \questao :

(?:.|\s)*?(\begin{.*?})(?:\s\end{enumerate}\s*?)(\questao.*)

You can see how it works here

Explanation:

  • (?:.|\s)*? This regex ignores all content until a valid capture occurs through a non-capture group

02.04.2018 / 15:59