你的位置:首页 > 软件开发 > ASP.net > c# 正则表达式 匹配中括号颜色过滤

c# 正则表达式 匹配中括号颜色过滤

发布时间:2016-08-28 14:00:03
现在需要匹配 [color=#000000],以"[color"开头,以"]"结束,中间字符数量不限制,最后返回所有匹配的下标。代码如下: 1 /// <summary> 2 /// 取得所有匹配项的下 ...

现在需要匹配 [color=#000000],以"[color"开头,以"]"结束,中间字符数量不限制,最后返回所有匹配的下标。

代码如下:

 1     /// <summary>      2     /// 取得所有匹配项的下标 3     /// </summary>      4     /// <param name="source">原内容</param>      5     /// <returns>返回匹配列表的下标</returns>      6     public static int[] GetColorIndexGroup(get='_blank'>string source) 7     { 8       // 定义正则表达式用来匹配  9       Regex reg = new Regex("\\[color.*?\\]", RegexOptions.IgnoreCase);10 11       //将匹配到的项替换为空12       //var newSource = reg.Replace(source, "");13 14       // 搜索匹配的字符串       15       MatchCollection matches = reg.Matches(source);16 17       int i = 0;18       int[] colorGroup = new int[matches.Count];19       // 取得匹配项列表       20       foreach (Match match in matches)21       {22         colorGroup[i++] = match.Index;23       }24       return colorGroup;25     }

原标题:c# 正则表达式 匹配中括号颜色过滤

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录