下载地址: http://download.csdn.net/source/1805903 private string ReArrange(string str) { string[] keywords = new string[] {"select","from","where","and","order//s+by","or","into", "update","set","delete","haveing","group//s+by", "inner//s+join","left//s+join","right//s+join","on",","}; string reg_str1 = "//s+(?=//b" + keywords[0] + "//b)"; string reg_str2 = "(?<=//b" + keywords[0] + "//b)//s+"; for (int i = 1; i < keywords.Length; i++) { reg_str1 += "|//s+(?=//b" + keywords[i] + "//b)"; reg_str2 += "|(?<=//b" + keywords[i] + "//b)//s+"; } Regex reg_sel_b = new Regex(reg_str1, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline); Regex reg_sel_a = new Regex(reg_str2, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline); return (reg_sel_a.Replace(reg_sel_b.Replace(str, Environment.NewLine), Environment.NewLine + new string (' ',4) )); }