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

本文介绍了如何使用正则表达式进行数据验证,包括匹配指定格式的字符串如身份证号、手机号、日期时间及IP地址等,并提供了实际代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExpandedBlockStart.gif代码
 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!");
            }

 

 

验证手机号码

 

ExpandedBlockStart.gif代码
 string str = "15012345678";

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

 

 

 

ExpandedBlockStart.gif代码
 //匹配月份加时间
            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!");
            }

 

 

 

转载于:https://www.cnblogs.com/jhxk/articles/1835287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值