-2

I have a regular expression problem. My data is as follows:

[Section 1]
   title = RegEx
   name = Joe
   color = blue
[Section 2]
   height = 101
   name = Gray

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

user3557422
  • 29
  • 1
  • 4
  • To get you started: [**How to read and write ini files**](http://stackoverflow.com/questions/8884188/how-to-read-and-write-ini-file-with-python) – Jan Sep 01 '16 at 15:44

3 Answers3

0

Using ConfigParser is pretty simple, but you need to change your data format to be like:

config_file.cfg

[Section 1]
title: RegEx
name: Joe
color: blue
[Section 2]
height: 101
name: Gray

test_config.py

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']
Andrea
  • 583
  • 5
  • 15
  • I have tried the ConfigParser, only thing I didn't like what I had to change the format of the config file. I don't want to have to remove tabs and reinsert them on every subsection. – user3557422 Sep 01 '16 at 16:46
0

While I wouldn't do this with regular expressions, since you asked:

\[Section 1\][^[]*name\s*=\s*(.*)

The [^[] 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 [).

The result will be in the captured group.

https://regex101.com/r/uC7xD1/1

John
  • 2,395
  • 15
  • 21
0

Just for reference, you could use the newer regex module and named capture groups:

import regex as re

rx = re.compile("""
            (?(DEFINE)
               (?<section>^\[Section\ \d+\])
            )
            (?&section)
            (?:(?!(?&section))[\s\S])*
            ^\s*name\s*=\s*\K(?P<name>.+)$
            """, re.VERBOSE|re.MULTILINE)

string = """
[Section 1]
   title = RegEx
   name = Joe
   color = blue
[Section 2]
   height = 101
   name = Gray
"""

names = [match.group('name') for match in rx.finditer(string)]
print(names)
# ['Joe', 'Gray']

See a demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79