利用正则来查找字符串中第n个匹配字符索引

时间:2024-04-30 11:23:54

1.string.IndexOf()方法可以获得第一个匹配项的索引

2.要获取第n个匹配项的索引:
 方法1:利用IndexOf方法循环获取。
方法2:用正则来查找。
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(input, query);
通过:matches[count].Index获取
其中: input:表示待查找的字符串,
  1. query":匹配字符串,

             count":第几个匹配项,从0开始。