NullReference

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
用正则表达式保留系统靓号

有的时候用户系统用类似于QQ的号码做为UIN,这个时候可能需要保留鞋好的号码供以后不时之需,怎么实现呢?

正则就行了。看代码。

 

        public static Dictionary<string, Regex> _validations = new Dictionary<string, Regex>{
            {"6位顺增号", new Regex(@"(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}\d", RegexOptions.Compiled)},
            {"6位顺降好", new Regex(@"(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5}\d", RegexOptions.Compiled)},
            {"6位顺增或顺降号", new Regex(@"(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5})\d", RegexOptions.Compiled)},
            {"4-9位连续号",new Regex(@"(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){3,}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3,})\d", RegexOptions.Compiled)},
            {"3位以上重复号", new Regex(@"([\d])\1{2,}", RegexOptions.Compiled)},
            //{"日期号",new Regex(@"(19|20)[\d]{2}(1[0-2]|0?[1-9])(31|2[0-9]|1[0-9]|0?[0-9])", RegexOptions.Compiled)},
            //{"手机号码号", new Regex(@"(1[3,5,8][0-9]|15[0-9]|18[0-9])([\d]{2,4}){2}", RegexOptions.Compiled)},
            {"AABBB号", new Regex(@"([\d])\1{1,}([\d])\2{2,}", RegexOptions.Compiled)},
            {"ABBXABB号", new Regex(@"(([\d]){1,}([\d]){1,})\1{1,}", RegexOptions.Compiled)},
            {"AABBC,ABCDD号",new Regex(@"([\d])\1{1,}([\d])\2{1,}", RegexOptions.Compiled)},
        };

        public int GenerateUin()
        {
            var r = new Random((int)DateTime.Now.Ticks);
            var n = r.Next(1000000, 9999999);
            var tmp = "";
            var t = n.ToString();

            if (!IsGoodUin(t, out tmp)
                && !ObjectContext.Member.Any(m => m.UIN == t))
                return n;

            return GenerateUin();                
        }

        public static bool IsGoodUin(string uin, out string reason)
        {
            var isGood = false;
            var r1 = "";

            _validations.Each(r =>
            {
                if (r.Value.IsMatch(uin))
                {
                    isGood = true;
                    r1 = r.Key;
                    return false;
                }

                return true;
            });

            reason = r1;
            return isGood;
        }

测试以下:

 

        public void TestPresentUIN()
        {
            100.Each(() =>
            {
                Console.WriteLine(GenerateUin());
            });
        }

嗯,输出的号码都是歪瓜裂枣了。呵呵。

 

代码
3595046
7212339
8016182
8016182
8418104
8820025
9221947
9221947
9623868
1025791
1427712
1829634
1829634
2633477
3035398
3437320
3437320
3839241
4241163
4241163
4643084
5045006
5446927
5848849
6250770
6652692
7054613
7054613
7456534
7858456
8260377
8260377
9064220
9868063
9868063
1269986
1671907
1671907
2073829
2877672
2877672
4083436
4083436
4485358
6093044
6896887
6896887
7298808
8102651
8102651
8504573
8504573
8906494
9710337
9710337
1514181
1514181
1916103
2318024
2318024
2719946
3121867
3523789
3925710
5935317
5935317
6337239
7141082
7141082
7543003
7944925
7944925
8346846
8748768
9150689
9552611
9954532
1356455
1356455
1758376
2160298
2160298
2562219
3767984
3767984
4169905
4571827
4973748
5375670
5375670
6179513
6179513
6581434
6983356
7385277
7787199
8189120
8992963
8992963
9394885

 

 

 

 

posted on 2010-01-20 11:31  NullReference  阅读(1637)  评论(0编辑  收藏  举报