SoFunction
Updated on 2025-03-01

Summary of several commonly used regular expressions in C#

using System; 
using ; 

namespace CommonTools 

/**//// <summary> 
///  A summary description of RegexLib.
/// </summary> 
public class RegexLib 


//Verify the email address
public static bool IsValidEmail(string strIn) 

// Return true if strIn is in valid e-mail format. 
return (strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); 

//The date form of dd-mm-yy replaces the date form of mm/dd/yy.
public static string MDYToDMY(String input) 

return (input,"\\b(?\\d{1,2})/(?\\d{1,2})/(?\\d{2,4})\\b","${day}-${month}-${year}"); 

//Verify whether it is a decimal
public static bool IsValidDecimal(string strIn) 

return (strIn,@"[0].\d{1,2}|[1]"); 

//Verify whether it is a phone number
public static bool IsValidTel(string strIn) 

return (strIn,@"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?"); 

//Verify year, month, date
public static bool IsValidDate(string strIn) 

return (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)$"); 

//Verify the suffix name
public static bool IsValidPostfix(string strIn) 

return (strIn,@"\.(?i:gif|jpg)$"); 

//Verify whether the characters are between 4 and 12 more
public static bool IsValidByte(string strIn) 

return (strIn,@"^[a-z]{4,12}$"); 

//Verify IP
public static bool IsValidIp(string strIn) 

return (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])$");