Search code examples
pythonpython-re

regular expression can not find all


The following code can be run directly. What I want is to return a list: l = [1,2] (string). However, what I got is the string between the very first "begin" and the last "end". Even though this is one of the expected results. I can not find it out what happened.

import re

text = r'''

\begin{figure}
1
\end{figure}

aaa

\begin{figure}
2
\end{figure}

'''

pattern = r"\\begin{figure}([\s\S^f]*)\\end{figure}"
r = re.findall(pattern, text)


print(r)

Solution

  • The * operator captures as many characters as possible. This means it captures until the last occurence of \end{figure} If you only want to capture as many characters as needed, use *? instead: pattern = r"\\begin{figure}([\s\S^f]*?)\\end{figure}".