日期:2014-05-18 浏览次数:20798 次
string ss = "a1a2a3a4a5a6a7a8a9"; MatchCollection mc = Regex.Matches(ss, @"a\d"); for (int i = 0; i < mc.Count; i++) { Console.WriteLine(mc[i].Value); }
string[] r = new string[mc.Count]; for (int i = 0; i < mc.Count; i++) { r[i] = mc[i].Value; }
private string[] SplitStringArray() { System.Collections.ArrayList list = new System.Collections.ArrayList(); string ss = "a1a2a3a4a5a6a7a8a9"; System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(ss, @"a\d"); for (int i = 0; i < mc.Count; i++) { list.Add(mc[i].Value.ToString()); Console.WriteLine(mc[i].Value); } return (String[])list.ToArray(typeof(string)); }
------解决方案--------------------
没必要吧。。。
MatchCollection 的内容远远不止一个string[],做这种只能说华而不实,一定要做,自己写个方法,处理一下,单纯取$0的内容存到一个数组里,使用时调用这个方法也就是了
当然,对于一些简单的,可以用Regex.Split来做
string ss = "a1a2a3a4a5a6a7a8a9"; string[] result = Regex.Split(ss, @"(?<=a\d+)(?=a\d+)", RegexOptions.IgnoreCase);