title: Dhcpd lease matching by using regex in python?
body: <p>I want to match all dhcp leases that have given mac address. </p>
  
  <p>I wrote this code</p>
  
  <pre><code>fh = open(leaseFile)
  lines = fh.read()
  fh.close()
  regex = r"lease\s*[0-9\.]+\s*\{[^\{\}]*%s[^\{\}]*?\}" % mac #mac comes as parameter
  m = re.findall(regex,lines,re.DOTALL)
  </code></pre>
  
  <p>This worked well if a lease don't contain '}' character. But if it does, my regex failed.
  For example:</p>
  
  <pre><code>lease 10.14.53.253 {
    starts 3 2012/10/17 09:27:20;
    ends 4 2012/10/18 09:27:20;
    tstp 4 2012/10/18 09:27:20;
    binding state free;
    hardware ethernet 00:23:18:62:31:5b;
    uid "\001\000\013OW}k";
  }
  </code></pre>
  
  <p>I couldn't figure out how I handle this exception. Thanks for any advice...</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After research, I decided to use this regex with MULTILINE mode. It worked for all leases that I tried.</p>
  
  <pre><code>fh = open(leaseFile)
  lines = fh.read()
  fh.close()
  regex = r"lease\s*[0-9\.]+\s*\{[^\{\}]*%s[\s\S]*?^\}" % mac #mac comes as parameter
  m = re.findall(regex,lines,re.MULTILINE)
  </code></pre>