0

如何获取使用 regex.matches 中的每个迭代的匹配索引?

4

3 回答 3

0

集合中的每个Match对象都有一个Index属性。请参阅http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match.aspx

于 2012-10-22T04:12:26.087 回答
0

尝试如下

 string strInput = "YourString";

 Regex regex = new Regex("Your Regex to match");
 var m = regex.Match(strInput);
 if (m.Success)
      {
          foreach (var matches in regex.Matches(strInput))
           {
              if (m.Success)
                 {
                     Console.WriteLine(m.Index);
                 }

                 m = m.NextMatch();
            }
        }
于 2012-10-22T04:15:50.293 回答
0

Match.Index 将返回匹配项的第一个字符在原始字符串上的位置。

MatchCollection 只实现ICollection接口而不实现IList,所以我认为不可能直接从 matchcollection 中检索项目的索引。

但是您可以使用 aCollection.CopyTo将其移动到数组中。

http://msdn.microsoft.com/en-us/library/gg695671.aspx

Array 确实有一个IndexOf,您可以使用它来检索索引。

于 2012-10-22T04:17:06.523 回答