Extract specific section from LaTeX file with python
<p>I have a set of LaTeX files. I would like to extract the "abstract" section for each one: </p>

<pre class="lang-tex prettyprint-override"><code>\begin{abstract}

.....

\end{abstract}
</code></pre>

<p>I have tried the suggestion here: <a href="http://stackoverflow.com/questions/30752351/how-to-parse-latex-file">How to Parse LaTex file</a></p>

<p>And tried :</p>

<pre><code>A = re.findall(r'\\begin{abstract}(.*?)\\end{abstract}', data)
</code></pre>

<p>Where data contains the text from the LaTeX file. But <code>A</code> is just an empty list. Any help would be greatly appreciated!</p>

<p>The <code>.</code> does not match newline character.  However, you can pass a flag to ask it to include newlines.</p>

<p>Example:</p>

<pre><code>import re

s = r"""\begin{abstract}
this is a test of the
linebreak capture.
\end{abstract}"""

pattern = r'\\begin\{abstract\}(.*?)\\end\{abstract\}'

re.findall(pattern, s, re.DOTALL)

#output:
['\nthis is a test of the\nlinebreak capture.\n']
</code></pre>

python regex latex