在外部代码怎么用 Ngan.re_dict 来访问 Ngan 类的 Dictionary<string, Regex> redict (也需要这样来访问 Ngan.re_funcs),初衷是在某些流程里减少重复生成正则表达式。最好还是用 Dictionary 方式来储存(其他的不会...)
public class Ngan { public static Dictionary<Regex, Func<string, string>> re_funcs; public static Dictionary<string, Regex> re_dict; public Ngan() { // 读取正则表达式到缓存 load_re_dict(); load_re_funcs(); } private static void load_re_dict(){ re_dict = new Dictionary<string, Regex>(); Regex quote_re = new Regex(@"\[quote\](.*?)\[\/quote\]", RegexOptions.Multiline); Regex uid_re = new Regex(@"\[uid.*?\](.*?)\[\/uid\]"); Regex quoted_re = new Regex(@"\[\/b\](.*?)\[\/quote\]", RegexOptions.Multiline); Regex collapse_re = new Regex(@"\[collapse(=.*?)\](.*?)\[\/collapse\]", RegexOptions.Multiline); re_dict["quote"] = quote_re; re_dict["uid"] = uid_re; re_dict["quoted"] = quoted_re; re_dict["collapse"] = collapse_re; } private static void load_re_funcs(){ re_funcs = new Dictionary<Regex, Func<string, string>>(); // 处理回复内容 Regex reply_re = new Regex(@"Reply\sto.*?\[uid.*?\](.*?)\[\/uid\].*?\[\/b\]", RegexOptions.Multiline); Func<string, string> reply_act = (con) => String.Format( "<br><i><b>回复</b>{0}</i><br>", con); re_funcs.Add(reply_re, reply_act); // 链接 Regex link_re = new Regex(@"\[url\](.*?)\[\/url\]"); Func<string, string> link_act = (con) => { if (!con.StartsWith("http")){ con = "http://" + con; // 自动添加 http://前缀 } return ("<a href=\"" + con + "\" target=\"_blank\">链接</a>"); }; re_funcs.Add(link_re, link_act); } // ... } 