title: Sorting filenames using regular expressions
body: <p>I have a directory that contains 200 files (.txt) and I'm looking to sort them using a regular expression on a certain part of the filename.</p>
  
  <p>Example file names are:</p>
  
  <pre><code>W:/name/2014-09-02 - name/20140902-070343_1-P4.txt
  W:/name/2014-09-02 - name/20140902-070343_10-P4.txt
  W:/name/2014-09-02 - name/20140902-070343_2-P4.txt
  W:/name/2014-09-02 - name/20140902-070343_200-P4.txt
  </code></pre>
  
  <p>Which are in the same format in the array.</p>
  
  <p>The code below will store the filenames in an array. I'm then looking to sort using a regular expression on the <code>_1</code>, <code>_200</code>, <code>_10</code> <code>_2</code> part of the file name.</p>
  
  <pre><code>require 'FileUtils'
  contents = Dir.glob("W:/name/2014-09-02 - name/*.txt").select { |f| f.include?('P4')}.sort_by! {|s|  s[/(\D\d+)-P4.txt/]}
  puts contents
  </code></pre>
  
  <p>With the above code I'm getting:</p>
  
  <pre><code>_1
  _10
  _100
  _101
  _102
  .
  .
  .
  _11
  _111
  </code></pre>
  
  <p>When what I want is:</p>
  
  <pre><code>_1
  _2
  _3
  _4
  _5
  .
  .
  .
  _10
  _11
  </code></pre>
  
  <p>Ultimately, Ill then want to loop through the array to confirm the incrementing value is continuous (1 - 200).</p>