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

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开始。
 
 
 

猜你喜欢

转载自www.cnblogs.com/DiKingVue/p/11913258.html