2

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?

1 Answer 1

5

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't the curly braces as metacharacters be escaped with \{?
@halex It seems many parsers treat them as literals, if they don't have a number inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.