title: Matching an optional substring in a regex
body: <p>I'm developing an algorithm to parse a number out of a series of short-ish strings. These strings are somewhat regular, but there's a few different general forms and several exceptions. I'm trying to build a set of regexes that will handle the various forms and exceptions; I'll apply them one after another to see if I get a match.</p>
  <p>One of these forms goes something like this:</p>
  <pre><code>X (Y) Z
  </code></pre>
  <p>Where:</p>
  <ul>
  <li><code>X</code> is a number I want to capture.</li>
  <li><code>Z</code> is static, pre-defined text. it's basically how I determine whether this particular form is applicable or not.</li>
  <li><code>Y</code> is a string of unknown length and content, surrounded by parenthesis.</li>
  </ul>
  <p>Also: <code>Y</code> is optional; it doesn't always appear in a string with <code>Z</code> and <code>X</code>. So, I want to be able to extract the numbers from all of these strings:</p>
  <ul>
  <li><code>10 Z</code></li>
  <li><code>20 (foo) Z</code></li>
  <li><code>30 (bar) Z</code></li>
  </ul>
  <p>Right now, I have a regex that will capture the first one:</p>
  <pre><code>([0-9]+) +Z
  </code></pre>
  <p>My problem is that I don't know how to construct a regex that will match a series of characters if and only if they're enclosed in parenthesis. Can this be done in a single regex?</p>