正则表达式号称开发者得瑞士军刀,使用好正则表达式尤其重要。
拆分多个正则:
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); } Listresult = 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; }
有以上几个辅助类,在难得正则拆分都能搞定。