1
dictNum2 = {{"eins", 1}, {"zwei", 2}, {"drei", 3} ...};
foreach (KeyValuePair<string, int> dsa in dictNum2)
            {
                Regex regexTemp = new Regex(dsa.Key);
                MatchCollection matchTemp = regexTemp.Matches(stringInput);
            if ((stringInput.Contains(dsa.Key) && dsa.Value < 10))
                {
                    var indexList = Regex.Matches(stringInput, dsa.Key).Cast<Match>().Select(m => m.Index).ToList();
                    indexList.AddRange(indexList);
                    for(int i = 1; i < indexList.Count; i++)
                    {
                        if(indexList[i] == indexList[i-1] + dsa.Key.Length)
                        {
                            inaRow++;
                        }
                    }
                }
           }

The idea is: need to find the number of words following each other in a string, that contains in a dictionary. I have piece of code that works for something like "zweizweizwei", but on input could be string like that:

"zweihundertzweidreiundzwanzig" or "zweiunddreieins"

Is there way to solve that? Thanks

LFJ
  • 13
  • 2

2 Answers2

0

Use word boundaries in your regex

Regex regexTemp = new Regex($"\b{dsa.Key}\b")
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

For number of words captured by the regex you can use Captures

var dictNum2 = new Dictionary<string, int>() { { "eins", 1 }, { "zwei", 2 }, { "drei", 3 } };
string stringInput = "zweihundertzweidreidreidreiundzwanzig";

int inaRow = 0;
var regex = new Regex("(" + string.Join("|", dictNum2.Keys) + ")+");
foreach (Match m in regex.Matches(stringInput))
{
    inaRow = Math.Max(inaRow, m.Groups[1].Captures.Count);
}
// inaRow is 4

but in case you just want to know if there is duplicate, it is a bit easier

var regex = new Regex("(" + string.Join("|", dictNum2.Keys) + "){2}");
bool duplicate = regex.IsMatch(stringInput);
AntonĂ­n Lejsek
  • 6,003
  • 2
  • 16
  • 18