正则表达式起始符和结束符的作用和验证手机号码等

代码
 String Str = "1234567891012356565656565";

            Regex regex 
= new Regex(@"\d{6}");

            
//将匹配成功因为只要是6个数字的都匹配
            foreach (Match match in regex.Matches(Str))
            {
                Response.Write(match 
+ "<br/>");
            }

            
//匹配起始和末尾必须为6个数字
            regex = new Regex(@"^\d{6}$");

            
foreach (Match match in regex.Matches(Str))
            {
                Response.Write(match 
+ "<br/>");
            }

            String str1 
= "123456";
            
//其实和末尾为6个数字所以匹配成功.
            if (regex.IsMatch(str1))
            {
                Response.Write(
"Success!");
            }
            
else
            {
                Response.Write(
"Fail!");
            }

 

 

验证手机号码

 

代码
 string str = "15012345678";

            
if (Regex.IsMatch(str,@"^0?1(3\d|5[03689])\d{8}$"))
            {
                Response.Write(
"Success!");
            }
            
else
            {
                Response.Write(
"Fail!");
            }

 

 

 

代码
 //匹配月份加时间
            String str = "11月00:00";

            
if (Regex.IsMatch(str, @"^([1][0-2]|0?\d)月[0-2][0-3]:[0-5][0-9]$"))
            {
                Response.Write(
"Success!");
            }
            
else
            {
                Response.Write(
"Fail!");
            }

            
//IP地址字符串
            str = "192.168.1.1";

            
if (Regex.IsMatch(str, @"^((2[0-5][0-5]|1\d\d|\d\d|\d)\.){3}(2[0-5][0-5]|1\d\d|\d\d|\d)$"))
            {
                Response.Write(
"Success!");
            }
            
else
            {
                Response.Write(
"Fail!");
            }

 

 

 

posted @ 2010-09-26 09:25  唔愛吃蘋果  阅读(11342)  评论(0编辑  收藏  举报