幾個C#常用正則表達(dá)式的總結(jié)
更新時間:2006年08月24日 00:00:00 作者:
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])$");
}
}
}
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])$");
}
}
}
您可能感興趣的文章:
- 收集的ASP.NET中常用正則表達(dá)式
- 常用正則表達(dá)式與批注[收藏]
- [PHP]常用正則表達(dá)式收集
- 常用正則 常用的C#正則表達(dá)式
- 最常用的PHP正則表達(dá)式收集整理
- 常用正則表達(dá)式 整理篇
- PHP 正則表達(dá)式常用函數(shù)使用小結(jié)
- php中常用的正則表達(dá)式的介紹及應(yīng)用實例代碼
- JS的正則表達(dá)式常用檢測代碼
- jQuery源碼分析-02正則表達(dá)式 RegExp 常用正則表達(dá)式
- 正則表達(dá)式常用元字符整理小結(jié)
- 收集的一些常用java正則表達(dá)式
- python的正則表達(dá)式re模塊的常用方法
- 常用的JQuery數(shù)字類型驗證正則表達(dá)式整理
- PHP中過濾常用標(biāo)簽的正則表達(dá)式
- PHP和javascript常用正則表達(dá)式及用法實例
- Python常用正則表達(dá)式符號淺析
- 常用的正則表達(dá)式集錦
- 常用的正則表達(dá)式集錦
- 比較常用證件正則表達(dá)式驗證大全
- 常用正則表達(dá)式知識點解讀及判讀有效數(shù)字、手機(jī)號郵箱正則表達(dá)式
相關(guān)文章
一步步打造漂亮的新聞列表(無刷新分頁、內(nèi)容預(yù)覽)第二步
由于我們僅僅是項目中的一個小部分,但也差不多按照以上的順序進(jìn)行開發(fā),這是一個良好的習(xí)慣。我們將概要設(shè)計和詳細(xì)設(shè)計放在一起。2010-07-07
asp.net下SQLite(輕量級最佳數(shù)據(jù)庫) 原理分析和開發(fā)應(yīng)用
SQLite是一個開源的嵌入式關(guān)系數(shù)據(jù)庫,它在2000年由D. Richard Hipp發(fā)布,它的減少應(yīng)用程序管理數(shù)據(jù)的開銷,SQLite可移植性好,很容易使用,很小,高效而且可靠2011-10-10
System.Diagnostics.Metrics .NET 6 全新指標(biāo)API講解
本文詳細(xì)講解了.NET 6全新指標(biāo)System.Diagnostics.Metrics,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
Linux Ubuntu系統(tǒng)上手動安裝.NET Core SDK的方法
.NET Core是一個開源通用的開發(fā)框架,支持跨平臺,即支持在Window,macOS,Linux等系統(tǒng)上的開發(fā)和部署,并且可以在硬件設(shè)備,云服務(wù),和嵌入式/物聯(lián)網(wǎng)方案中進(jìn)行使用。下面這篇文章將給大家詳細(xì)介紹關(guān)于在Linux Ubuntu系統(tǒng)上手動安裝.NET Core SDK的方法。2016-12-12

