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>Using ConfigParser is pretty simple, but you need to change your data format to be like:</p>

<p>config_file.cfg</p>

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

<p>test_config.py</p>

<pre><code>import ConfigParser

def get_config(section, prop_file_path):
    config = ConfigParser.ConfigParser()
    config.read(prop_file_path)
    options = config.options(section)
    data = {}
    for option in options:
            try:
                data[option] = config.get(section, option)
            except:
                data[option] = None
                raise Exception("exception on %s!" % option)
    return data

data = get_config("Section 1", "path/to/file/config_file.cfg")
print data['name']
</code></pre>

python regex