title: How to split /etc/network/interfaces using a regex in Python
body: <p>I'm trying to break the <code>/etc/network/interfaces</code> file format on Ubuntu to the individual stanzas (as the man page calls them).</p>
  
  <p>This is the sample interfaces file I test my script against:</p>
  
  <pre><code># The loopback network interface
  auto lo
  iface lo inet loopback
  
  auto eth0
  iface eth0 inet static
      address 192.168.2.7
      netmask 255.255.255.0
      network 192.168.2.0
      broadcast 192.168.2.255
      gateway 192.0.2.254
      dns-nameservers 12.34.56.78 12.34.56.79
  
  auto eth0:0
  allow-hotplug eth0:0
  iface eth0:0 inet static
      address 192.168.1.43
      netmask 255.255.255.0
  
  auto eth1
  iface eth1 inet dhcp
  
  auto eth2
  iface eth2 inet6 static
      address 2001:db8::c0ca:1eaf
      netmask 64
      gateway 2001:db8::1ead:ed:beef
  
  auto br0
  iface br0 inet static
      address 10.10.0.15
      netmask 255.255.255.0
      gateway 10.10.0.1
      bridge_ports eth0 eth1
      up /usr/sbin/brctl stp br0 on
  </code></pre>
  
  <p>What I need is an array of strings holding each stanza (<code>iface</code>, <code>mapping</code>, <code>auto</code>, <code>allow-\w+</code>, <code>source(-\w+)?</code> and comments) along with all the text that follows it until the start of next stanza.</p>
  
  <p>I've tried code like this, which sounds like it should work, but it captures all stanzas in one string:</p>
  
  <pre><code>re.split(r'^(iface|mapping|auto|allow-\w+|source(-\w)?|#.*)[.\n]+?',
      open('/etc/network/interfaces').read(), flags=re.MULTILINE)
  </code></pre>
  
  <p>How can I correct the regex to achieve this?</p>
  
  <p>Python version is 2.7</p>