亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C# 數(shù)據(jù)驗(yàn)證Regex示例詳解

 更新時(shí)間:2025年02月12日 09:42:27   作者:幻想趾于現(xiàn)實(shí)  
文章介紹了C#中使用Regex進(jìn)行數(shù)據(jù)驗(yàn)證的方法,包括整數(shù)和小數(shù)的正負(fù)驗(yàn)證,以及郵箱和身份證號(hào)的格式驗(yàn)證,感興趣的朋友一起看看吧

Regular Expression,簡(jiǎn)稱 Regex,是一種用于匹配和處理文本的強(qiáng)大工具。它通過定義特定的模式,可以用來搜索、替換或提取字符串中的特定內(nèi)容。

先引入命名空間

using System.Text.RegularExpressions;

Intege(整數(shù))

必須是正整數(shù)

        //必須是正整數(shù)
        public static bool IsPositiveInteger(string txt)
        {
            Regex objReg = new Regex(@"^[1-9]\d*$");
            return objReg.IsMatch(txt);
        }
 

正整數(shù)和零

 
        public static bool IsPositiveIntegerAndZero(string txt)
        {
            Regex objReg = new Regex(@"^[1-9]\d*|0$");
            return objReg.IsMatch(txt);
        }
 

負(fù)整數(shù)

 
        public static bool IsNegativeInteger(string txt)
        {
            Regex objReg = new Regex(@"^-[1-9]\d*$");
            return objReg.IsMatch(txt);
        }

正負(fù)均可

 
        public static bool IsInteger(string txt)
        {
            Regex objReg = new Regex(@"^-?[1-9]\d*$");
            return objReg.IsMatch(txt);
        }

Decimal(小數(shù))

正數(shù)

 
        public static bool IsPositiveDecimal(string txt)
        {
            Regex objReg = new Regex(@"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$");
            return objReg.IsMatch(txt);
        }

負(fù)數(shù)

 
        public static bool IsNegativeDecimal(string txt)
        {
            Regex objReg = new Regex(@"^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$");
            return objReg.IsMatch(txt);
        }
 

正負(fù)均可

 
        public static bool IsDecimal(string txt)
        {
            Regex objReg = new Regex(@"^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$");
            return objReg.IsMatch(txt);
        }

其他驗(yàn)證

郵箱

 
        public static bool IsEmail(string txt)
        {
            Regex objReg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return objReg.IsMatch(txt);
        }

身份證

 
        public static bool IsIdentityCard(string txt)
        {
            Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
            return objReg.IsMatch(txt);
        }

郵箱編碼

   public static bool IsPostalCode(string txt)
        {
            if (txt.Length != 6) return false;
            Regex objReg = new Regex(@"[1-9]\d{5}(?!\d)");
            return objReg.IsMatch(txt);
        }

到此這篇關(guān)于C# 數(shù)據(jù)驗(yàn)證Regex的文章就介紹到這了,更多相關(guān)C# 數(shù)據(jù)驗(yàn)證Regex內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論