Regular expression to find text in specific section
<p>I have a regular expression problem.  My data is as follows:</p>

<pre><code>[Section 1]
   title = RegEx
   name = Joe
   color = blue
[Section 2]
   height = 101
   name = Gray
</code></pre>

<p>My question is can I write a regular expression to capture the 'name' key only from [Section 1]?  Essentially, capture a key that may exist in multiple places, but only capture it from a specific section.  I'll be implementing this in python.
Thanks</p>

<p>While I wouldn't do this with regular expressions, since you asked:</p>

<pre><code>\[Section 1\][^[]*name\s*=\s*(.*)
</code></pre>

<p>The <code>[^[]</code> bit prevents the regular expression from being too greedy and matching a "name" outside of the specified section (assuming no other fields/lines within a section include a <code>[</code>).</p>

<p>The result will be in the captured group.</p>

<p><a href="https://regex101.com/r/uC7xD1/1" rel="nofollow">https://regex101.com/r/uC7xD1/1</a></p>

python regex