1

So I have the below method that will scan through a list of words, finds "Control Number:" and sets it to wordNumber, then it sets the next word to controlNum (which is the string I am looking to return).

public string ABSFindControlNumber(List<tessnet2.Word> wordList)
        {
    for (int i = 0; i < wordList.Count; i++)
                    {
                        if (wordList[i].Text == "Control Number:" && wordList[i].Confidence >= 50)
                        {

                            string wordNumber = wordList[i].Text.ToString();
                            controlNum = wordList[i + 1].Text.ToString();
                            return controlNum;
        }
        }
}

But after finding out how to do with a similar approach using RegEx. I want to see if there is a way to set controlNum to the next word. I have a few different cases for certain letters/numbers just in case it doesn't find the exact word.

if (Regex.IsMatch(text, @"c(0|o)ntr(0|o)(l|1|i)\s+nu(in|m)ber(:|;|s)", RegexOptions.IgnoreCase))
 {
                controlNum = ???
 }
MaylorTaylor
  • 4,671
  • 16
  • 47
  • 76

1 Answers1

1

You can do this:

string text = "Control Number: 123foobar";
var match = Regex.Match(text, @"c[o0]ntr[o0][l1i]\s+nu(?:in|m)ber[:;s]\s*(\w*)", RegexOptions.IgnoreCase);
if (match.Success)
{
    var controlNum = match.Groups[1].Value; // 123foobar
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • This didn't seem to work exactly right. If I leave the code exactly as you have it, controlNum will be '123foobar'. However, if I remove '123foobar' from the `string text` then controlNum will be just "". ... I wont know the control number before this function. – MaylorTaylor Jul 11 '13 at 18:25
  • @MaylorTaylor Presumably you're reading a block of text in from *somewhere* and then trying to parse it out to get the control number, right? Well my `text = "Control Number: 123foobar"` is just some sample input for demonstration purposes. You would actually have to get that from whatever text you're trying to read. – p.s.w.g Jul 11 '13 at 18:36