溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#常用正則表達(dá)式有哪些

發(fā)布時間:2021-09-09 16:49:20 來源:億速云 閱讀:105 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)C#常用正則表達(dá)式有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

using System;  
using System.Text.RegularExpressions;  
namespace CommonTools  
{  
/**//// <summary>  
/// RegexLib 的摘要說明。  
/// </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)證是否為電話號碼  
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])$");  
}  
}  
}

感謝各位的閱讀!關(guān)于“C#常用正則表達(dá)式有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI