用正則表達式保留系統靚號

有的時候用戶系統用類似於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());
    });
}

嗯,輸出的號碼都是歪瓜裂棗了。呵呵。

轉自:http://www.cnblogs.com/nullreference/archive/2010/01/20/keep_present_uin.html

複製代碼
代碼
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

發佈了32 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章