日期:2014-05-20 浏览次数:20844 次
using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { string str = "<You're angle & evil>"; string pattern = "'|&|<|>"; Regex regex = new Regex(pattern); Program prog = new Program(); MatchEvaluator evaluator = new MatchEvaluator(prog.ConvertToXML); Console.WriteLine(regex.Replace(str, evaluator)); Console.Read(); } //把正则表达式的匹配到的字符转换成xml能正常识别的标识 public string ConvertToXML(Match m) { //string s0=m.Groups[0].Value; //string s1=m.Groups[1].Value; //string s2=m.Groups[2].Value; switch (m.Value) { case "'": return "&apos"; case "&": return "&"; case "<": return "<"; case ">": return ">"; default: return ""; } } }
string str = "<You're angle & evil>"; string pattern = "['&<>]"; //str = Regex.Replace(str, pattern, new MatchEvaluator(delegate(Match m) { // or str = Regex.Replace(str, pattern, m => { switch (m.Value) { case "'": return "&apos"; case "&": return "&"; case "<": return "<"; case ">": return ">"; default: return ""; } });
------解决方案--------------------
struct UInt96 { ulong hi; uint lo; // 构造函数 public UInt96(ulong h, uint l) { hi = h; lo = l; } // 返回加1后的结果 public UInt96 Inc() { if (lo < 0xFFFFFFFF) return new UInt96(hi, lo + 1); return new UInt96(hi + 1, 0); } // 自增运算符 public static UInt96 operator ++(UInt96 x) { return x.Inc(); } // 十六进制字符串表示 public override string ToString() { return string.Join(" ", Regex.Split(string.Format("{0:X16}{1:X8}", hi, lo), "(?!^)(?=(?:.{4})+$)")); } }