在C#中查找字符串中的所有模式索引

san*_*p22 4 c# regex string indexing

如何使用c#查找字符串中模式的所有索引?

例如,我想##在这样的字符串中找到所有模式索引45##78$$#56$$JK01UU

Pra*_*nam 9

 string pattern = "##";
 string sentence = "45##78$$#56$$J##K01UU";
 IList<int> indeces = new List<int>();
 foreach (Match match in Regex.Matches(sentence, pattern))
 {
      indeces.Add(match.Index);
 }
Run Code Online (Sandbox Code Playgroud)

indeces将有2,14