# To match only innermost environment by Regex

I want to match the innermost environment of begin{question} and its corresponding end{question}.

Example data

\section{Takayasu arteritis}

\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}

\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}

Fever of unknown origin can be used when you do not know what is causing the disease.

% Show cases in MedScape and ask class.

Aneurysms.

\subsection{Treatment}

\begin{question}
{What you should always include in Takayasu treatment?
What are the symptoms?}
Blood pressure.
Aneurysms which will burst without treatment.
So blood pressure decreasing drugs like beta blockers along in combination with other drugs.
\end{question}


My expected output is

\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}


or

\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}


or

\begin{question}
{What you should always include in Takayasu treatment?
What are the symptoms?}
Blood pressure.
Aneurysms which will burst without treatment.
So blood pressure decreasing drugs like beta blockers along in combination with other drugs.
\end{question}


How can you match only the innermost environment?

• What's your expected output? – Avinash Raj Oct 4 '14 at 20:50
• @AvinashRaj Thank you for your comment! I added the expected output. – Léo Léopold Hertz 준영 Oct 4 '14 at 20:52
• This regex will do the job (?s)\\begin{question}.*?\\end{question} but i don't know how to implement it in Perl. – Avinash Raj Oct 4 '14 at 20:55
• Here pcregrep -M '(?s)\\begin{question}.*?\\end{question}' file – Avinash Raj Oct 4 '14 at 21:24

Try this:

pcregrep -M '\\begin{question}(.|\n)*?\\end{question}'


Explanation:

• pcregrep: grep with Perl-compatible regular expressions
• -M: Allow patterns to match more than one line
• (.|\n)*?: any normal character . or new line \n matched zero or more times ., in non-greedy mode ?.

Result:

\begin{question}
{You get a patient.
What do you notice first in this patient?}
Absence of peripheral pulse.
\end{question}
\begin{question}
{What was the first Takayasu case?}
Young woman in Asia with red vessels in the eye.
So special eye diagnosis done.
Affects eye.
\end{question}
\begin{question}
{What you should always include in Takayasu treatment?
What are the symptoms?}
Blood pressure.
Aneurysms which will burst without treatment.
So blood pressure decreasing drugs like beta blockers along in combination with other drugs.
\end{question}

• even more simpler pcregrep -M '\\begin{question}[\S\s]*?\\end{question}' file – Avinash Raj Oct 4 '14 at 21:22

Do you need it to be a pure regex solution, or just perlish?

perl -lne 'print if(/^\\begin{question}/ .. /^\\end{question}/)'  file