常用的正则表达式验证类
生活随笔
收集整理的這篇文章主要介紹了
常用的正则表达式验证类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
搜集的一些常用的正則表達式驗證類:
using?System;?using?System.Text.RegularExpressions;?
?
namespace?CommonTools?
{?
????/**//**//**////?<summary>?
????///?RegexLib?的摘要說明。?
????///?</summary>?
????public?class?RegexLib?
????{?
?????????????
????????????//驗證Email地址?
????????????public?static?bool?IsValidEmail(string?strIn)?
????????????{?
????????????????//?Return?true?if?strIn?is?in?valid?e-mail?format.?
????????????????return?Regex.IsMatch(strIn,?@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");??
????????????}?
????????????//dd-mm-yy?的日期形式代替?mm/dd/yy?的日期形式。?
????????????public?static?string?MDYToDMY(String?input)??
????????????{?
????????????????return?Regex.Replace(input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}");?
????????????}?
????????????//驗證是否為小數(shù)?
????????????public?static?bool?IsValidDecimal(string?strIn)?
????????????{?
????????????????return?Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]");??
????????????}?
????????????//驗證是否為電話號碼?
????????????public?static?bool?IsValidTel(string?strIn)?
????????????{?
????????????????return?Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");??
????????????}?
????????????//驗證年月日?
????????????public?static?bool?IsValidDate(string?strIn)?
????????????{?
????????????????return?Regex.IsMatch(strIn,@"^2\d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]\d|3[0-1])(?:0?[1-9]|1\d|2[0-3]):(?:0?[1-9]|[1-5]\d):(?:0?[1-9]|[1-5]\d)$");??
????????????}?
????????????//驗證后綴名?
????????????public?static?bool?IsValidPostfix(string?strIn)?
????????????{?
????????????????return?Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$");??
????????????}?
????????????//驗證字符是否再4至12之間?
????????????public?static?bool?IsValidByte(string?strIn)?
????????????{?
????????????????return?Regex.IsMatch(strIn,@"^[a-z]{4,12}$");??
????????????}?
????????????//驗證IP?
????????????public?static?bool?IsValidIp(string?strIn)?
????????????{?
????????????????return?Regex.IsMatch(strIn,@"^(\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])$");??
????????????}?????????????
?
????}?
}?
總結
以上是生活随笔為你收集整理的常用的正则表达式验证类的全部內容,希望文章能夠幫你解決所遇到的問題。