Regex로 가장 안쪽 환경만 일치시키려면

Regex로 가장 안쪽 환경만 일치시키려면

나는 의 가장 안쪽 환경 begin{question}과 그에 상응하는 end{question}.

예시 데이터

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

내 예상 결과는

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

어떻게 가장 안쪽 환경만 일치시킬 수 있나요?

답변1

이 시도:

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

설명:

  • pcregrep: Perl 호환 정규 표현식을 사용하여 grep
  • -M: 패턴이 두 줄 이상 일치하도록 허용합니다.
  • (.|\n)*?: non-greedy 모드에서 일반 문자 .나 새 줄이 \n0번 이상 일치합니다 ..?

결과:

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

답변2

순수한 정규식 솔루션이 필요합니까, 아니면 단지 Perlish여야 합니까?

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

관련 정보