c# 正则表达式 匹配中括号&颜色过滤
2016-08-28 13:24
921 查看
现在需要匹配 [color=#000000],以"[color"开头,以"[/color]"结束,中间字符数量不限制,最后返回所有匹配的下标。
代码如下:
代码如下:
/// <summary> /// 取得所有匹配项 /// </summary> /// <param name="source">原内容</param> /// <returns>返回匹配列表的下标</returns> public static int[] GetColorIndexGroup(string source) { // 定义正则表达式用来匹配 Regex reg = new Regex("\\[color.*?\\].*?\\[/color\\]", RegexOptions.IgnoreCase); //将匹配到的项替换为空 //var newSource = reg.Replace(source, ""); // 搜索匹配的字符串 MatchCollection matches = reg.Matches(source); int i = 0; //存放下标 int[] colorIndexGroup = new int[matches.Count]; //存放匹配到的内容 string[] colorContentGroup = new string[matches.Count]; // 取得匹配项列表 foreach (Match match in matches) { colorContentGroup[i] = match.Value; colorIndexGroup[i++] = match.Index; } return colorIndexGroup; }
相关文章推荐
- C#匹配首尾中括号的正则表达式
- 正则表达式中匹配字符中括号'['
- c#中过滤html的正则表达式
- 在C#中使用正则表达式自动匹配并获取所需要的数据
- 用正则表达式过滤脚本(js)的一些研究(asp.net + C#)
- C#中可以使用正则表达式来过滤html字符
- C#中几个正则表达式匹配输入字符的函数
- c#中过滤html的正则表达式
- [C#][正则表达式]寻找匹配的Groups的几种方法
- 在C#中使用正则表达式过滤html字符
- Python用re(正则表达式)模块过滤Telnet字体颜色
- C#中正则表达式的完全匹配
- C#正则表达式寻找匹配的Groups的几种方法
- [C#][正则表达式]寻找匹配的Groups的几种方法
- 用正则表达式过滤脚本的一些研究(asp.net + C#)
- 在C#中使用正则表达式自动匹配并获取所需要的数据
- [C#][正则表达式]寻找匹配的Groups的几种方法
- [C#][正则表达式]寻找匹配的Groups的几种方法
- C#中获取匹配正则表达式的字符
- [C#][正则表达式]寻找匹配的Groups的几种方法