title: Find the longest sequence of digits in a string
body: <p>I am trying to clear up the results for poor quality OCR reads, attempting to remove everything I can safely assume is a mistake.</p>
  
  <p>The desired result is a 6 digit numerical string, so I can rule out any character that isn't a digit from the results. I also know these numbers appear sequentially, so any numbers out of sequence are also very likely to be incorrect.</p>
  
  <p>(Yes, fixing the quality would be best but no... they won't/can't change their documents)</p>
  
  <p>I immediately <code>Trim()</code> to remove white space, also as these are going to end up as file names I also remove all illegal characters.</p>
  
  <p>I've found out which Characters are digits and added them to a dictionary against the array position in which they where found.
  This leaves me with a clear visual indication of the number sequencies but I am struggling on the logic of how to get my program to recognise this.</p>
  
  <p>Tested with the string "<strong>Oct', 2$3622</strong>" (an actual bad read)
  The ideal output for this would be "<strong>3662</strong>"
  <img src="https://i.stack.imgur.com/yZjVm.jpg" alt="Obvious to Humans"></p>
  
  <pre><code>    public String FindLongest(string OcrText)
      {
          try
          {
              Char[] text = OcrText.ToCharArray();
              List&lt;char&gt; numbers = new List&lt;char&gt;();
  
              Dictionary&lt;int, char&gt; consec = new Dictionary&lt;int, char&gt;();
  
              for (int a = 0; a &lt; text.Length; a++)
              {
                  if (Char.IsDigit(text[a]))
                  {
                      consec.Add(a, text[a]);
  
                      // Won't allow duplicates?
                      //consec.Add(text[a].ToString(), true);
                  }
              }
  
              foreach (var item in consec.Keys)
              {
                  #region Idea that didn't work
                  // Combine values with consecutive keys into new list
                  // With most consecutive?
                  for (int i = 0; i &lt; consec.Count; i++)
                  {
                      // if index key doesn't match loop, value was not consecutive
                      // Ah... falsely assuming it will start at 1. Won't work.
                      if (item == i)
                          numbers.Add(consec[item]);
                      else
                          numbers.Add(Convert.ToChar("#")); //string split value
                  }
                  #endregion
              }
  
              return null;
          }
          catch (Exception ex)
          {
              string message;
  
              if (ex.InnerException != null)
                  message =
                      "Exception: " + ex.Message +
                      "\r\n" +
                      "Inner: " + ex.InnerException.Message;
              else
                  message = "Exception: " + ex.Message;
              MessageBox.Show(message);
  
              return null;
          }
      }
  </code></pre>