title: a regular expression generator for number ranges
body: <p>I checked on the stackExchange description, and algorithm questions are one of the allowed topics. So here goes.</p>
  
  <p>Given an input of a range, where begin and ending numbers have the same number of digits (say, 2, 3, or 4), I want to write code to generate a set of regular expressions which, when checked against a number in turn, tell me whether that number is in the original range.</p>
  
  <p>For example: if the range is 145-387, then 146, 200, and 280 would all match one of the regular expressions generated, and 144, 390 (used to say 290), and 445 (used to say 345) would not.</p>
  
  <p>I have been thinking the result would be a list of regular expressions like:</p>
  
  <pre><code>14[5-9]             // match 145-149
  1[5-9]0-9]          // 150-199
  2[0-9][0-9]         // 200-299
  3[0-7][0-9]         // 300-379
  38[0-7]             // 380-387
  </code></pre>
  
  <p>and then software checking the number would test to see if the 3-digit code being tested matched any of these.</p>
  
  <p>So what's the best way to generate the set of expressions?</p>
  
  <p>The latest (in a series) that I've come up with is to:</p>
  
  <ol>
  <li>determine the first digit at which the two range numbers differ (1145-1158, first different digit is the 3rd)</li>
  <li>for the different digits, determine if their first digits differ by more than one -- if so, the range for that gets its own regex (200-299 in our example)</li>
  <li>to get lower ranges: for each other digit: prefix by the first digit(s) from the beginning of the range, increment the digit by one, pad with 0s to the same length, and pair with a number that has 9 in the digit place and all the padded places. In our example, increment 4 to 5, pad to get 150, generate the regex to handle 150-199.</li>
  <li>to get higher ranges: for each other digit: prefix with first digit(s) from end of range, decrement digit by one, pad rest with 0s, pair with a number with 9s in all the padded 0 places and the decremented digit. In our example, the regex to handle 300-379.</li>
  </ol>
  
  <p>Am I missing something? There are details even in the above that I'm glossing over, it seems like something that would benefit from an algorithmic sword slashing through the details. But the other things I've come up with are even messier than this.</p>