I am trying to parse /etc/network/interfaces config file in Ubuntu so I need divide string into list of strings where each string begins with one of the given keywords.
According to manual:
The file consists of zero or more "iface", "mapping", "auto", "allow-" and "source" stanzas.
So If the file contains:
auto lo eth0
allow-hotplug eth1
iface eth0-home inet static
address 192.168.1.1
netmask 255.255.255.0
I would like to get list:
['auto lo eth0', 'allow-hotplug eth1', 'iface eth0-home inet static\n address...']
Now I have function like this:
def get_sections(text):
start_indexes = [s.start() for s in re.finditer('auto|iface|source|mapping|allow-', text)]
start_indexes.reverse()
end_idx = -1
res = []
for i in start_indexes:
res.append(text[i: end_idx].strip())
end_idx = i
res.reverse()
return res
But it isn't nice...