幾個(gè)C#常用正則表達(dá)式的總結(jié)
更新時(shí)間:2006年08月24日 00:00:00 作者:
using System;
using System.Text.RegularExpressions;
namespace CommonTools
{
/**//// <summary>
/// RegexLib 的摘要說(shuō)明。
/// </summary>
public class RegexLib
{
//驗(yàn)證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}");
}
//驗(yàn)證是否為小數(shù)
public static bool IsValidDecimal(string strIn)
{
return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]");
}
//驗(yàn)證是否為電話號(hào)碼
public static bool IsValidTel(string strIn)
{
return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
}
//驗(yàn)證年月日
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)$");
}
//驗(yàn)證后綴名
public static bool IsValidPostfix(string strIn)
{
return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$");
}
//驗(yàn)證字符是否再4至12之間
public static bool IsValidByte(string strIn)
{
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
}
//驗(yàn)證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 的摘要說(shuō)明。
/// </summary>
public class RegexLib
{
//驗(yàn)證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}");
}
//驗(yàn)證是否為小數(shù)
public static bool IsValidDecimal(string strIn)
{
return Regex.IsMatch(strIn,@"[0].\d{1,2}|[1]");
}
//驗(yàn)證是否為電話號(hào)碼
public static bool IsValidTel(string strIn)
{
return Regex.IsMatch(strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
}
//驗(yàn)證年月日
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)$");
}
//驗(yàn)證后綴名
public static bool IsValidPostfix(string strIn)
{
return Regex.IsMatch(strIn,@"\.(?i:gif|jpg)$");
}
//驗(yàn)證字符是否再4至12之間
public static bool IsValidByte(string strIn)
{
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
}
//驗(yàn)證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)用實(shí)例代碼
- JS的正則表達(dá)式常用檢測(cè)代碼
- jQuery源碼分析-02正則表達(dá)式 RegExp 常用正則表達(dá)式
- 正則表達(dá)式常用元字符整理小結(jié)
- 收集的一些常用java正則表達(dá)式
- python的正則表達(dá)式re模塊的常用方法
- 常用的JQuery數(shù)字類型驗(yàn)證正則表達(dá)式整理
- PHP中過(guò)濾常用標(biāo)簽的正則表達(dá)式
- PHP和javascript常用正則表達(dá)式及用法實(shí)例
- Python常用正則表達(dá)式符號(hào)淺析
- 常用的正則表達(dá)式集錦
- 常用的正則表達(dá)式集錦
- 比較常用證件正則表達(dá)式驗(yàn)證大全
- 常用正則表達(dá)式知識(shí)點(diǎn)解讀及判讀有效數(shù)字、手機(jī)號(hào)郵箱正則表達(dá)式
相關(guān)文章
一步步打造漂亮的新聞列表(無(wú)刷新分頁(yè)、內(nèi)容預(yù)覽)第二步
由于我們僅僅是項(xiàng)目中的一個(gè)小部分,但也差不多按照以上的順序進(jìn)行開(kāi)發(fā),這是一個(gè)良好的習(xí)慣。我們將概要設(shè)計(jì)和詳細(xì)設(shè)計(jì)放在一起。2010-07-07
asp.net下SQLite(輕量級(jí)最佳數(shù)據(jù)庫(kù)) 原理分析和開(kāi)發(fā)應(yīng)用
SQLite是一個(gè)開(kāi)源的嵌入式關(guān)系數(shù)據(jù)庫(kù),它在2000年由D. Richard Hipp發(fā)布,它的減少應(yīng)用程序管理數(shù)據(jù)的開(kāi)銷,SQLite可移植性好,很容易使用,很小,高效而且可靠2011-10-10
System.Diagnostics.Metrics .NET 6 全新指標(biāo)API講解
本文詳細(xì)講解了.NET 6全新指標(biāo)System.Diagnostics.Metrics,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
asp.net UpdaeProgress的簡(jiǎn)單用法
這個(gè)控件相比其他控件,屬性少 使用簡(jiǎn)單,就先把這個(gè)控件的一般使用方法簡(jiǎn)單紀(jì)錄下2008-10-10
Linux Ubuntu系統(tǒng)上手動(dòng)安裝.NET Core SDK的方法
.NET Core是一個(gè)開(kāi)源通用的開(kāi)發(fā)框架,支持跨平臺(tái),即支持在Window,macOS,Linux等系統(tǒng)上的開(kāi)發(fā)和部署,并且可以在硬件設(shè)備,云服務(wù),和嵌入式/物聯(lián)網(wǎng)方案中進(jìn)行使用。下面這篇文章將給大家詳細(xì)介紹關(guān)于在Linux Ubuntu系統(tǒng)上手動(dòng)安裝.NET Core SDK的方法。2016-12-12

