有的时候用户系统用类似于QQ的号码做为UIN这个时候可能需要保留鞋好的号码供以后不时之需怎么实现呢? 正则就行了看代码 view sourceprint?public static Dictionary<string Regex> _validations = new Dictionary<string Regex>{ {位顺增号 new Regex(@(?:(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)){}\d RegexOptionsCompiled)} {位顺降好 new Regex(@(?:(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)){}\d RegexOptionsCompiled)} {位顺增或顺降号 new Regex(@(?:(?:(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)){}|(?:(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)){})\d RegexOptionsCompiled)} {位连续号new Regex(@(?:(?:(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)){}|(?:(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)|(?=)){})\d RegexOptionsCompiled)} {位以上重复号 new Regex(@([\d])\{} RegexOptionsCompiled)} //{日期号new Regex(@(|)[\d]{}([]|?[])(|[]|[]|?[]) RegexOptionsCompiled)} //{手机号码号 new Regex(@([][]|[]|[])([\d]{}){} RegexOptionsCompiled)} {AABBB号 new Regex(@([\d])\{}([\d])\{} RegexOptionsCompiled)} {ABBXABB号 new Regex(@(([\d]){}([\d]){})\{} RegexOptionsCompiled)} {AABBCABCDD号new Regex(@([\d])\{}([\d])\{} RegexOptionsCompiled)} }; public int GenerateUin() { var r = new Random((int)DateTimeNowTicks); var n = rNext( ); var tmp = ; var t = nToString(); if (!IsGoodUin(t out tmp) && !ObjectContextMemberAny(m => mUIN == t)) return n; return GenerateUin(); } public static bool IsGoodUin(string uin out string reason) { var isGood = false; var r = ; _validationsEach(r => { if (rValueIsMatch(uin)) { isGood = true; r = rKey; return false; } return true; }); reason = r; return isGood; } 测试以下 view sourceprint?public void TestPresentUIN() { Each(() => { ConsoleWriteLine(GenerateUin()); }); } 嗯输出的号码都是歪瓜裂枣了呵呵
|