title: Regex fun. Why doesn't this regex match my string?
body: <p>I'm debugging carrierwave. It's fun. I think the problem comes down to this:</p>
  
  <p>Inside carrierwave_direct, there's this code:</p>
  
  <pre><code>if record.new_record? &amp;&amp; record.send("has_#{attribute}_upload?") &amp;&amp; record.key !~ record.send(attribute).key_regexp
  </code></pre>
  
  <p>The #key_regexp code is this:</p>
  
  <pre><code>  /\A#{store_dir}\/[a-f\d\-]+\/.+\.(?i)#{extension_regexp}(?-i)\z/
  </code></pre>
  
  <p>which evaluates to this when I am debugging:</p>
  
  <pre><code>record.send(attribute).key_regexp
  /\Astories\/\/main_images\/[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i)\z/
  </code></pre>
  
  <p>My store_dir is this:</p>
  
  <blockquote>
    <p>"stories/#{model.id}/main_images"</p>
  </blockquote>
  
  <p>and I think there's a blank "/" section because model.id is nil.</p>
  
  <p>The record.key is this:</p>
  
  <blockquote>
    <p>"/stories/main_imagescache/20160319-1549-2202-5826/blueapronimage.jpg"</p>
  </blockquote>
  
  <p>Why doesn't it currently match?</p>
  
  <p>It looks like the regex has an extra slash and I'm not sure what this section is:</p>
  
  <pre><code>[a-f\d\-]+\/.+\.(?i)(jpg|jpeg|gif|png)(?-i)
  </code></pre>
  
  <p>Is it any number of letters between a-f, digit, or "-", then a "/", then any number of letters or numbers, then a "."....</p>
  
  <p>What is the (?i)?</p>
  
  <p>What are capture groups?</p>
  
  <p>What is the (?-i)?</p>
  
  <p>I think this is the problem:</p>
  
  <pre><code>record.key !~ /\Astories/
  true
  </code></pre>
  
  <p>It should be false.</p>