title: PHP Regex on CD Tracklists
body: <p>I'm using <code>preg_match</code> to format tracklists so the track number, title and duration are separated into their own cells in a table:</p>
  
  <pre><code>&lt;td&gt;01&lt;/td&gt;&lt;td&gt;Track Title&lt;/td&gt;&lt;td&gt;01:23&lt;/td&gt;
  </code></pre>
  
  <p>The problem is that the tracks themselves can take any of the following forms (the leading zeros on the track numbers and durations are not always present):</p>
  
  <pre><code>01. Track Title (01:23)
  01. Track Title 01:23
  01. Track Title
  1 Track Title (01:23)
  1 Track Title 01:23
  1 Track Title
  </code></pre>
  
  <p>The following only works on tracks with a timestamp:</p>
  
  <pre><code>/([0-9]+)\.?[\s+](.*)[\s+](\?[0-5]?[0-9]:[0-5][0-9]\)?)/
  </code></pre>
  
  <p>So I added <code>?</code> to the timestamp:</p>
  
  <pre><code>/([0-9]+)\.?[\s+](.*)[\s+]((\?[0-5]?[0-9]:[0-5][0-9]\)?)?/
  </code></pre>
  
  <p>This then works for tracks without a timestamp, but tracks with a timestamp end up with the timestamp stuck with the title, like so:</p>
  
  <pre><code>&lt;td&gt;01&lt;/td&gt;&lt;td&gt;Track Title 01:23&lt;/td&gt;&lt;td&gt;&lt;/td&gt;
  </code></pre>
  
  <p>EDIT: The tracklists are plaintext and are being pulled from an SQL table before parsing.</p>