 <p><strong>Use this:</strong></p> <pre><code>(?s)&lt;Device(?:(?!&lt;\/Device).)*&lt;Block[^&gt;]+Name="ZG123CZ123".*?&lt;\/Device&gt; </code></pre> <p><sub><a href="http://regex101.com/r/bU5zC1/1" rel="nofollow"><em>Demo</em></a></sub></p> <hr> <p><strong>Explanation:</strong></p> <ul> <li><code>(?s)</code> is the single-line modifier, which makes <code>.</code> include newline characters.</li> <li><code>&lt;Device</code> looks for the beginning of a <code>Device</code> element.</li> <li><code>(?:(?!&lt;\/Device).)*</code> is essentially <code>.*</code>, but each time we try to match a character we make sure we aren't matching the end of a <code>Device</code> element (<code>&lt;\/Device</code>) by using a negative lookahead. Note that you can't just make this a lazy match, since it would continue until if found the <code>&lt;Block Name="ZG123CZ123"&gt;</code> (that is the next chunk).</li> <li><code>&lt;Block[^&gt;]+Name="ZG123CZ123"</code> will look for the beginning of a <code>Block</code> element, followed by non <code>&gt;</code> character(s), followed by the <code>Name</code> attribute equaling <code>ZG123CZ123</code>.</li> <li><code>.*?</code> is a lazy match to eat up the rest of the <code>Device</code> element.</li> <li><code>&lt;\/Device&gt;</code> we've made it to the end!</li> </ul>