本文實(shí)例講述了C#常用正則驗(yàn)證函數(shù)。分享給大家供大家參考,具體如下:
1、Ip地址驗(yàn)證
/// <summary>/// Ip地址驗(yàn)證/// </summary>public static bool CheckIp(string ip){ bool result = false; Regex ipReg = new Regex(@"^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])$"); if (ipReg.IsMatch(ip)) { result = true; } return result;}2、價(jià)格驗(yàn)證
/// <summary>/// 價(jià)格驗(yàn)證/// </summary>/// <param name="priceStr"></param>/// <returns></returns>public bool CheckPrice(string priceStr){ bool result = false; Regex regex = new Regex(@"^/d+(/./d{1,2})?$", RegexOptions.IgnoreCase); Match match = regex.Match(priceStr); if (match.Success) { result = true; } return result;}3、正整數(shù)驗(yàn)證
/// <summary>/// 正整數(shù)驗(yàn)證/// </summary>public static bool CheckPositiveInteger(string numStr){ bool result = false; Regex regex = new Regex(@"^[1-9]/d*$", RegexOptions.IgnoreCase); Match match = regex.Match(numStr); if (match.Success) { result = true; } return result;}PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測(cè)試工具:
http://tools.VeVB.COm/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.VeVB.COm/regex/create_reg
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#正則表達(dá)式用法總結(jié)》、《C#編碼操作技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》及《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選