title: Ruby - Regex for matching brackets?
body: <p>I am trying to figure out if a string has properly closed brackets.</p>
  
  <p>To do this, I use the following three bracket pairs.</p>
  
  <pre><code>[]
  ()
  {}
  </code></pre>
  
  <p>The brackets may also be nested as long as they're formatted properly.</p>
  
  <pre><code>)([]{} - Does not have properly closed brackets because )( is reverse order
  
  [()] - Does contain properly closed brackets.
  </code></pre>
  
  <p>I've tried using regex and after a bit of fumbling around, I got this.</p>
  
  <pre><code>[^\(\[]*(\(.*\))[^\)\]]*
  </code></pre>
  
  <p>However, there are a few problems with this.</p>
  
  <p>It only matches parentheses but doesn't match brackets</p>
  
  <p>I don't understand why it didn't match the brackets.</p>
  
  <p>In my examples I clearly used a backslash before the brackets.</p>
  
  <p>Input</p>
  
  <pre><code>[] - true
  [()] - true (nested brackets but they match properly)
  {} - true
  }{ - false (brackets are wrong direction)
  }[]} - false (brackets are wrong direction)
  [[]] - true (nested brackets but they match properly
  </code></pre>