博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#正则Groups高级使用方法
阅读量:5250 次
发布时间:2019-06-14

本文共 5602 字,大约阅读时间需要 18 分钟。

正则表达式号称开发者得瑞士军刀,使用好正则表达式尤其重要。

拆分多个正则:

public static string[] SplitByManyRegex(string text, string[] subRegexStrings)        {            string allRegexString = "^(?
.*?)"; for (int i = 0; i < subRegexStrings.Length; i++) { allRegexString += "(?
" + subRegexStrings[i] + ".*?)"; } allRegexString += "$"; Regex subRegex = new Regex(allRegexString, RegexOptions.Singleline | RegexOptions.IgnoreCase); MatchCollection mc = subRegex.Matches(text); if (mc.Count <= 0) { return new string[] { text }; } List
positions = new List
(); for (int m = 0; m < subRegexStrings.Length + 1; m++) { positions.Add(mc[0].Groups["mySubGroup" + m].Index); } List
result = new List
(); for (int i = 0; i < positions.Count; i++) { int nextPos = 0; if (i < positions.Count - 1) nextPos = positions[i + 1]; else nextPos = text.Length; result.Add(text.Substring(positions[i], nextPos - positions[i])); } return result.ToArray(); }

  调用:

string[] tags = { "【答案】", "【解析】" };

  拆分单个正则:

public static string[] SplitByRegex(string text, string subRegexString)        {            Regex subRegex = new Regex(subRegexString, RegexOptions.Singleline | RegexOptions.IgnoreCase);            MatchCollection mc = subRegex.Matches(text);            if (mc.Count <= 0)            {                return new string[] { text };            }            List
positions = new List
(); for (int m = 0; m < mc.Count; m++) { positions.Add(mc[m].Index); } List
result = new List
(); result.Add(text.Substring(0, positions[0])); for (int i = 0; i < positions.Count; i++) { int nextPos = 0; if (i < mc.Count - 1) nextPos = positions[i + 1]; else nextPos = text.Length; result.Add(text.Substring(positions[i], nextPos - positions[i])); } return result.ToArray(); }

  不反回第一条:

public static string[] SplitByRegexNoFirtPart(string text, string subRegexString)        {            string[] ary = SplitByRegex(text, subRegexString);            return TrimFirstElementOfArray(ary);        } private static string[] TrimFirstElementOfArray(string[] ary)        {            if (ary == null || ary.Length == 0) return new string[0];            string[] result = new string[ary.Length - 1];            for (int i = 1; i < ary.Length; i++) result[i - 1] = ary[i];            return result;        }

  拆分如:(A(B(C?)?)?)

public static string[] SplitByManyRegex_MayLess(string text, string[] subRegexStrings)        {            string allRegexString = "^(?
.*?)"; for (int i = 0; i < subRegexStrings.Length; i++) { allRegexString += "((?
" + subRegexStrings[i] + ".*?)"; } for (int i = subRegexStrings.Length-1; i >=0 ; i--) { allRegexString += "?)"; } allRegexString += "$"; Regex subRegex = new Regex(allRegexString, RegexOptions.Singleline | RegexOptions.IgnoreCase); MatchCollection mc = subRegex.Matches(text); if (mc.Count <= 0) { return new string[] { text }; } List
positions = new List
(); for (int m = 0; m < subRegexStrings.Length + 1; m++) { if (mc[0].Groups["mySubGroup" + m].Success) { positions.Add(mc[0].Groups["mySubGroup" + m].Index); } } List
result = new List
(); for (int i = 0; i < positions.Count; i++) { int nextPos = 0; if (i < positions.Count - 1) nextPos = positions[i + 1]; else nextPos = text.Length; result.Add(text.Substring(positions[i], nextPos - positions[i])); } return result.ToArray(); }

  可以任意顺序,任意个数:

public static string[] SplitByManyRegex_AnyOrder(string text, string[] subRegexStrings, bool resultChangeOrder = true )        {            if(string.IsNullOrEmpty(text) || subRegexStrings==null || subRegexStrings.Length == 0)            {                return new string[] { text };            }            string allReg = "(" + string.Join("|", subRegexStrings) + ")";            string[] result = SplitByRegex(text, allReg);            if (!resultChangeOrder) return result;             string[] ordered = new string[subRegexStrings.Length+1];            ordered[0] = result[0];            for(int i=1; i

  用正则表达式替换文本中的内容:

public static string TranformHandAnswer(string html)        {            string strReg = "(?
(
(.*?)
))"; //正则表达式 Regex regex = new Regex(strReg, RegexOptions.Singleline | RegexOptions.IgnoreCase); int _subjectOrderNum = subjectOrderNum; //TODO: Lambda不允许ref变量,这里临时这样用 html = regex.Replace(html, (Match match) => { string handContent = match.Groups["hand"].Value; string result = “替换得文本” return result; }); return html; }

  有以上几个辅助类,在难得正则拆分都能搞定。

转载于:https://www.cnblogs.com/sunliyuan/p/9245208.html

你可能感兴趣的文章
mongodb命令----批量更改文档字段名
查看>>
CentOS 简单命令
查看>>
使用&nbsp;SharedPreferences 分类: Andro...
查看>>
TLA+(待续...)
查看>>
题解: [GXOI/GZOI2019]与或和
查看>>
MacOS copy图标shell脚本
查看>>
国外常见互联网盈利创新模式
查看>>
Oracle-05
查看>>
linux grep 搜索查找
查看>>
Not enough free disk space on disk '/boot'(转载)
查看>>
android 签名
查看>>
vue项目中使用百度统计
查看>>
android:scaleType属性
查看>>
SuperEPC
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
三维变换概述
查看>>