分享多個C#常用正規表示式的實例

Y2J
發布: 2017-04-26 13:43:16
原創
1485 人瀏覽過

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}");  
}  
//验证是否为小数  
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])$");  
}  
}  
}
登入後複製

以上是分享多個C#常用正規表示式的實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!