Question: I need some clarification on how regex with the re.findall method works. 

```
pattern = re.compile(r'(?<=\\\\\[-16pt]\n)([\s\S]*?)(?=\\\\\n\\thinhline)')
content= ' '.join(re.findall(pattern, content))

```

So the above prints all the content that the pattern matches with the starting being: `\\[-16pt]` and the ending being '\ \n thinhline' plus all the text after it.
If I had the following content that matched by the pattern:

```
\\[-16pt]
x = 10
print ("hi")
\\
\thinhline
\\[-16pt]
y = 3
print ("bye")
\\
\thinhline
\\[-16pt]
z = 7
print ("zap")
\\
\thinhline
This is random text.
All of this is matched by re.findall, even though it is not included within the pattern.
xyz = "xyz"

```

How would I separate out each group so I could have, for example and be able to edit them independently:

group 1:

```
x = 10
print ("hi")

```

group 2:

```
y = 3
print ("bye")

```

group 3:

```
z = 7
print ("zap")

```

and none of the extra stuff that is matched after it?

Thank you.

Answer: 
Consider the following runnable program:

```
import re

content="""\\[-16pt]
x = 10
print ("hi")
\\
thinhline
\\[-16pt]
y = 3
print ("bye")
\\
thinhline
\\[-16pt]
z = 7
print ("zap")
\\
thinhline
This is random text.
"""

pattern = re.compile(r"""(\\\[-16pt]\n)    # Start. Don't technically need to capture.
                         (.*?)             # What we want. Must capture ;)
                         (\n\\\nthinhline) # End. Also don't really need to capture
                      """, re.X | re.DOTALL)

for m in pattern.finditer(content):
    print("Matched:\n----\n%s\n----\n" % m.group(2))

```

Output when run:

```
Matched:
----
x = 10
print ("hi")
----

Matched:
----
y = 3
print ("bye")
----

Matched:
----
z = 7
print ("zap")
----

```

Notes:

* By using the `re.X` option the expression can be multiline and commented
* By using the `re.DOTALL` option the excessive backslashes can be dropped
and the "`.*?`" group (i.e. "get every character non-greedily up until the 
next match") will include newlines.
* I used `finditer` rather than `findall` ... which technically moves away
from your question, but you wanted to work with each match so I figured it
was a good approach.
* I took the tab `\t` off the `thinhline` because I wasn't sure if it was
meant to be a tab char or a backlash-then-t. Not that affects the above much
but just wanted to be clear.
* I capture the start and end groups only for demonstration. Only the middle
group is really needed.
```
import re
s=re.findall(r"(?<=\\\\\[-16pt]\n)([\s\S]*?)(?=\\\\\n\\thinhline)",test_str)

```

Findall returns a list of groups matched.

Here `s` is a list.You can access what you want by referring `s[0]` or `s[1]`.