title: RegEx for ranges
body: <p>I want to be able to use a RegEx to parse out ranges like a Windows Print dialog (such as 1-50,100-110,111,112). The following is my current code and I am not clear on how to parse for the additional commas and numbers. I can parse out the hyphen, but not sure how to do it for additional commas or hyphens</p>
  
  <pre><code>private void tboxRowNum_Leave(object sender, EventArgs e)
  {
      Regex.Replace(tboxRowNum.Text, @"(?&lt;first&gt;\d+)-(?&lt;last&gt;\d+)",
      new MatchEvaluator(this.parseSpaceDefinition));
  
  }
  
  private string parseSpaceDefinition(Match m)
  {
      int first = int.Parse(m.Groups["first"].Value);
      int last = int.Parse(m.Groups["last"].Value);
      StringBuilder sb = new StringBuilder(first.ToString());
  
      for (int i = first + 1; i &lt;= last; i++)
      {
          if (spaceItems == 0)
          {
              if (isNumeric(sb.ToString(),  System.Globalization.NumberStyles.Integer))
              {
                  startingSpace = Convert.ToInt32(sb.ToString());
              }
          }
  
          sb.Append("," + i.ToString().Replace(" ", ""));
  
          spaceItems++;
          endingSpace = i;
      }
  
      tboxRowDesc.Text = sb.ToString();
      return sb.ToString();
  }
  </code></pre>
  
  <p><strong>Edit 1:</strong> The modified code gets me what I want:</p>
  
  <pre><code>private void tboxRowNum_Leave(object sender, EventArgs e)
  {
      string[] parts = tboxRowNum.Text.Split(',');
  
      for (int i = 0; i &lt; parts.Length; i++)
      {
          if (parts[i].IndexOf('-') &gt;= 0)
          {
              Regex.Replace(parts[i], @"(?&lt;first&gt;\d+)-(?&lt;last&gt;\d+)",
              new MatchEvaluator(this.parseSpaceDefinition));
          }
          else
          {
              int number;
              if(!(int.TryParse(parts[i], out number))) 
              {
                  MessageBox.Show("Incomplete/Invalid formula", "Invalid Space Definition");
                  tboxRowDesc.Text = "";
              }
              else 
              {
                  tboxRowDesc.Text += "," + number;
                  spaceItems++;
              }
          }
      }
  }
  </code></pre>