熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2013-11-13 09:46:01  來源: .NET編程 

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

  });

  }

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

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  


From:http://tw.wingwit.com/Article/program/net/201311/11535.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.