加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

几个C#常用正则表达式的汇总

发布时间:2023-04-11 13:58:32 所属栏目:语言 来源:
导读:using System;

using System.Text.RegularExpressions;

namespace CommonTools

{

/**//// <summary>

/// RegexLib 的摘要说明。

/// </summary>

public class RegexLib

{

//验证
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])$");
 
}
 
}
 
}
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!