C# 數(shù)據(jù)驗(yàn)證Regex示例詳解
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)文章
C#遞歸實(shí)現(xiàn)顯示文件夾及所有文件并計(jì)算其大小的方法
這篇文章主要介紹了C#遞歸實(shí)現(xiàn)顯示文件夾及所有文件并計(jì)算其大小的方法,是遍歷算法中比較典型的一種應(yīng)用,有不錯(cuò)的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08C#使用RichTextBox實(shí)現(xiàn)替換文字及改變字體顏色功能示例
這篇文章主要介紹了C#使用RichTextBox實(shí)現(xiàn)替換文字及改變字體顏色功能,結(jié)合實(shí)例形式洗了C#中RichTextBox組件文字替換及改變字體顏色相關(guān)操作技巧,需要的朋友可以參考下2019-02-02一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx
一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04c#批量抓取免費(fèi)代理并且驗(yàn)證有效性的實(shí)戰(zhàn)教程
突破反爬蟲限制的方法之一就是多用幾個(gè)代理IP,下面這篇文章主要給大家介紹了關(guān)于利用c#批量抓取免費(fèi)代理并且驗(yàn)證有效性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07C#實(shí)現(xiàn)將Excel表格轉(zhuǎn)換為圖片(JPG/?PNG)
Excel表格可能會(huì)因?yàn)椴煌O(shè)備或字體缺失等問題,導(dǎo)致格式錯(cuò)亂或數(shù)據(jù)顯示異常,轉(zhuǎn)換為圖片后,能確保數(shù)據(jù)的排版等保持一致,下面我們看看如何使用C#實(shí)現(xiàn)將Excel表格轉(zhuǎn)換為圖片吧2025-04-04unity實(shí)現(xiàn)鼠標(biāo)經(jīng)過時(shí)ui及物體的變色操作
這篇文章主要介紹了unity實(shí)現(xiàn)鼠標(biāo)經(jīng)過時(shí)ui及物體的變色操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04