How to extract content between a prefix and a suffix?
<p>I want to extract text from {inside} the curly brackets. The differences between those texts are the prefixes, such as <code>\section{</code> or <code>\subsection{</code> to categorize everything accordingly. And every end needs to be set by the next closed curly bracket <code>}</code>.</p>

<pre><code>file = "This is a string of an \section{example file} used for \subsection{Latex} documents."

# These are some Latex commands to be considered:

heading_1 = "\\\\section{"
heading_2 = "\\\\subsection{"

# This is my attempt.

for letter in file:
    print("The current letter: " + letter + "\n")
</code></pre>

<p>I want to process a Latex file by using Python to convert it for my database.</p>

<p>I think you want to use the regular expression module. </p>

<pre><code>import re

s = "This is a string of an \section{example file} used for \subsection{Latex} documents."

pattern = re.compile(r'\\(?:sub)?section\{(.*?)\}')
re.findall(pattern, s)

#output:
['example file', 'Latex']
</code></pre>

python latex