正则表达式通俗 用正则表达式保留系统靓号
用正则表达式保留系统靓号
有的时候用户系统用类似于QQ的号码做为UIN 这个时候可能需要保留鞋好的号码供以后不时之需 怎么实现呢?
正则就行了 看代码
view sourceprint?public static Dictionary<string Regex> _validations = new Dictionary<string Regex>{
{ 位顺增号 new Regex(@ (?: (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )){ }d RegexOptions Compiled)}
{ 位顺降好 new Regex(@ (?: (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )){ }d RegexOptions Compiled)}
{ 位顺增或顺降号 new Regex(@ (?:(?: (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )){ }|(?: (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )){ })d RegexOptions Compiled)}
{ 位连续号 new Regex(@ (?:(?: (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )){ }|(?: (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )| (?= )){ })d RegexOptions Compiled)}
{ 位以上重复号 new Regex(@ ([d]) { } RegexOptions Compiled)}
//{ 日期号 new Regex(@ ( | )[d]{ }( [ ]| ?[ ])( | [ ]| [ ]| ?[ ]) RegexOptions Compiled)}
//{ 手机号码号 new Regex(@ ( [ ][ ]| [ ]| [ ])([d]{ }){ } RegexOptions Compiled)}
{ AABBB号 new Regex(@ ([d]) { }([d]) { } RegexOptions Compiled)}
{ ABBXABB号 new Regex(@ (([d]){ }([d]){ }) { } RegexOptions Compiled)}
{ AABBC ABCDD号 new Regex(@ ([d]) { }([d]) { } RegexOptions Compiled)}
};
public int GenerateUin()
{
var r = new Random((int)DateTime Now Ticks);
var n = r Next( );
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 r = ;
_validations Each(r =>
{
if (r Value IsMatch(uin))
{
isGood = true;
r = r Key;
return false;
}
return true;
});
reason = r ;
return isGood;
}
测试以下
view sourceprint?public void TestPresentUIN()
{
Each(() =>
{
Console WriteLine(GenerateUin());
});
}
嗯 输出的号码都是歪瓜裂枣了 呵呵

lishixinzhi/Article/program/net/201311/11535