欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

先引入命名空間

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);
        }
 

負整數(shù)

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

正負均可

 
        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);
        }

負數(shù)

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

正負均可

 
        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);
        }

其他驗證

郵箱

 
        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);
        }

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

相關文章

最新評論