Dheerendra
Dheerendra

Reputation: 1568

Parsing Tex using python re library

I want to parse the following part of a .tex file

\section{a}
some random lines with lot 
of special characters
\subsection{aa}
somehthing here too
\section{b}

I want the content within \section{a} and \section{b} inclusive so i tried the following code in python

import re
a="my tex string mentioned above"
b=re.findall(r'\\section{a}.*\\section{b}',a)
print(b)

but i got b=[]. Where I am wrong?

Upvotes: 2

Views: 103

Answers (1)

xorsyst
xorsyst

Reputation: 8247

You need to use the re.DOTALL flag to make the . match newlines, like this:

b=re.findall(r'\\section{a}.*\\section{b}',a,re.DOTALL)

Upvotes: 5

Related Questions