.Net常用正则判断方法
生活随笔
收集整理的這篇文章主要介紹了
.Net常用正则判断方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
.Net常用正則判斷方法 原文:.Net常用正則判斷方法 /// <summary>/// 判斷string類型否為數字/// </summary>/// <param name="strNumber"></param>/// <returns></returns>public static bool IsNumber(string strNumber){string strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";string strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";return !Regex.IsMatch(strNumber, "[^0-9.-]") &&!Regex.IsMatch(strNumber, "[0-9]*[.][0-9]*[.][0-9]*") &&!Regex.IsMatch(strNumber, "[0-9]*[-][0-9]*[-][0-9]*") &&Regex.IsMatch(strNumber, "(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");}/// <summary>/// 判斷string類型否為正整數+0/// </summary>/// <param name="strNumber"></param>/// <returns></returns>public static bool IsPositive(string strNumber) => Regex.IsMatch(strNumber, "^\\d+$");/// <summary>/// 判斷string類型否為金額/// </summary>/// <param name="strNumber"></param>/// <returns></returns>public static bool IsAmount(string strNumber) => Regex.IsMatch(strNumber, "^[0-9]+(.[0-9]{2})?$");/// <summary>/// 判斷string類型否為手機號/// </summary>/// <param name="strPhone"></param>/// <returns></returns>public static bool IsPhone(string strPhone) => Regex.IsMatch(strPhone, "^0?(13[0-9]|15[012356789]|18[012356789]|14[012356789]|17[012356789])[0-9]{8}$");/// <summary>/// 判斷string類型否為固定電話號/// </summary>/// <param name="strTel"></param>/// <returns></returns>public static bool IsTel(string strTel) => Regex.IsMatch(strTel, "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$");/// <summary>/// 判斷string類型否為郵編/// </summary>/// <param name="strZipCode"></param>/// <returns></returns>public static bool IsZipCode(string strZipCode) => Regex.IsMatch(strZipCode, "[0-9]{6}");/// <summary>/// 判斷string類型否為Email/// </summary>/// <param name="strEmail"></param>/// <returns></returns>public static bool IsEmail(string strEmail) => Regex.IsMatch(strEmail, "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");/// <summary>/// 判斷是否為日期/// </summary>/// <param name="dateStr"></param>/// <returns></returns>public static bool IsDateString(string dateStr) => DateTime.TryParse(dateStr, out var date); View Code
?
posted on 2019-03-05 08:32 NET未來之路 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/lonelyxmas/p/10474506.html
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的.Net常用正则判断方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python slice() 函数
- 下一篇: u3d:动态的用代码调节材质球的属性