How to use REGEX with multiple filters
<p>There are three DAYs described by <code>text</code> variable: </p>

<pre><code>text = """
DAY {
 foo 12 5 A
 foo 
 12345
}
DAY {
 day 1
 day 2
 file = "/Users/Shared/docs/doc.txt"
 day 3
 end of the month
}
DAY {
 01.03.2016 11:15
 01.03.2016 11:16
 01.03.2016 11:17
}"""
</code></pre>

<p>All three DAY definitions begin with the word DAY (at the beginning of line), then a space and a curly bracket. The end is indicated with the closing bracket always placed at the beginning of the line.
So we can say the boundaries of each DAY is defined within the curly brackets {}. </p>

<p>Using <code>regex</code> I need to "find" the DAY that contains <code>file = "/Users/Shared/docs/doc.txt"</code> line inside of its boundary. </p>

<p>I started writing a regex expression:</p>

<p><code>string = """DAY {\n [A-Za-z0-9]+}"""</code></p>

<p><code>result = re.findall(string, text)</code></p>

<p>But the expression stops finding the text at the end of <code>foo</code> right before the white space character. How to modify the expression so it returns the second DAY that has <code>file = "/Users/Shared/docs/doc.txt"</code> in its body, so the result would look like:</p>

<pre><code>DAY {
 day 1
 day 2
 file = "/Users/Shared/docs/doc.txt"
 day 3
 end of the month
}
</code></pre>

<p>To perform regular expression matching on multiline text, you need to compile your regex with parameter <code>re.MULTILINE</code>.</p>

<p>This piece of code should work as you requested.</p>

<pre><code>regex = re.compile("""(DAY\s*\{[^\{\}]*file\ \=\ \"/Users/Shared/docs/doc\.txt\"[^\{\}]*\})""", re.MULTILINE)
regex.findall(text)
</code></pre>

<p>Result:</p>

<pre><code>['DAY {\n day 1\n day 2\n file = "/Users/Shared/docs/doc.txt"\n day 3\n end of the month\n}']
</code></pre>

python regex