在字符串后找到下一个单词

时间:2013-07-11 18:02:55

标签: c# regex ocr tessnet2

所以我有下面的方法将扫描一个单词列表,找到“控制号:”并将其设置为wordNumber,然后它将下一个单词设置为controlNum(这是我想要返回的字符串)。

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;
        }
        }
}

但是在找到如何使用RegEx的类似方法之后。我想看看是否有办法将controlNum设置为下一个单词。对于某些字母/数字,我有一些不同的情况,以防它找不到确切的单词。

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

1 个答案:

答案 0 :(得分:1)

你可以这样做:

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
}