protected void Page_Load(object sender, EventArgs e) { string str = "abcqqqqqabchahhabcaaabcssssdbsssabcaadcsssabc"; string key = "abc"; MatchCollection mc; Regex r = new Regex(key); // 定義一個Regex對象實例 mc = r.Matches(str); List<int> matchposition = new List<int>(); for (int i = 0; i < mc.Count; i++) //在字符串中找到全部匹配 { var s = mc[i].Value; //將匹配的字符串添在字符串數組中 if (s == key) { matchposition.Add(mc[i].Index); //記錄匹配字符的位置 } } for (int j = 0; j < matchposition.Count(); j++) { Response.Write(matchposition[j] + "<br />"); } }