1 string x = "Find somebody to love me, Find somebody to love"; 2 Regex r = new Regex(@"^Find ([a-z]{8}) to ([a-z]{4}) me, Find \1 to \2$"); 3 // r.Matches(x).Count --> 1 4 // Groups[0]是整个匹配的字符串,Groups[1]是组内的内容 5 // r.Matches(x).Groups[1].Value --> somebody 6 7 Regex r2 = new Regex(@"^Find (?<group1>[a-z]{8}) to ([a-z]{4}) me, Find \1 to \2$"); 8 // r2.Matches(x).Groups["group1"].Value 9 10 string y = "Find somebody somebody"; 11 regex r3 = new Regex(@"(?<g1>[a-z]+) \1"); 12 if (r3.IsMatch(y)) 13 { 14 y = r3.Replace(y, "${g1}"); 15 }
Regex reg = new Regex(regPattern, RegexOptions.IgnoreCase);
原文:http://www.cnblogs.com/bicker/p/5244987.html