C#正则Groups高级使用方法

网友投稿 604 2022-08-31

C#正则Groups高级使用方法

C#正则Groups高级使用方法

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

拆分多个正则:

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; }

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Go语言(环境的搭建)
下一篇:SqlServer性能优化:创建性能监视器(二)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~