天天看點

利用正規表達式 進行字元的判斷

using System;

using System.Text.RegularExpressions;

using System.Net;

namespace 正規表達式檢測字元串

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("請輸入字元串:");

            string s = Console.ReadLine();

            if (GF_IsOk.IsExistHanZi(s))

            {

                Console.Write("包含漢字");

            }

            else

                Console.Write("不包含漢字");

            Console.ReadLine();

        }

    }

    //判斷部分

    public class GF_IsOk

        /// <summary>

        /// 判讀是否是IP位址

        /// </summary>

        /// <param name="in_str"></param>

        /// <returns></returns>

        public static bool IsIPStr(string in_str)

            IPAddress ip;

            return IPAddress.TryParse(in_str, out ip);

        /// 判斷是否是數字

        /// <param name="strNumber"></param>

        public static bool IsNumber(string strNumber)

            Regex objNotNumberPattern = new Regex("[^0-9.-]");

            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");

            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");

            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";

            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";

            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");

            return !objNotNumberPattern.IsMatch(strNumber) &&

                   !objTwoDotPattern.IsMatch(strNumber) &&

                   !objTwoMinusPattern.IsMatch(strNumber) &&

                   objNumberPattern.IsMatch(strNumber);

        ///  判斷是否是日期字元串

        public static bool IsDateStr_yyyymmdd(string in_str)

            if (in_str == "") return true;

            if (in_str.Length != 8) return false;

            return IsDateStr(in_str);

        /// 判斷是否是日期字元串

        public static bool IsDateStr(string in_str)

            if (in_str.Length == 8)

                in_str = in_str.Substring(0, 4) + "-" + in_str.Substring(4, 2) + "-" + in_str.Substring(6, 2);

            DateTime dtDate;

            bool bValid = true;

            try

                dtDate = DateTime.Parse(in_str);

            catch (FormatException)

                // 如果解析方法失敗則表示不是日期性資料

                bValid = false;

            return bValid;

        /// 判斷字元串中是否包含漢字,有傳回true 否則為false

        /// <param name="str"></param>

        public static bool IsExistHanZi(string str)

            Regex reg = new Regex(@"[\u4e00-\u9fa5]");//正規表達式

            if (reg.IsMatch(str))

                return true;

                return false;

        /// 字段串是否為Null或為""(空)

        public static bool IsStrNullOrEmpty(string str)

            if (str == null || str.Trim() == string.Empty)

            return false;

        /// 傳回檔案是否存在

        /// <param name="filename">檔案名</param>

        /// <returns>是否存在</returns>

        public static bool IsFileExists(string filename)

            return System.IO.File.Exists(filename);

        /// 檢測是否符合email格式

        /// <param name="strEmail">要判斷的email字元串</param>

        /// <returns>判斷結果</returns>

        public static bool IsValidEmail(string strEmail)

            return Regex.IsMatch(strEmail, @"^[\w\.]+([-]\w+)*@[A-Za-z0-9-_]+[\.][A-Za-z0-9-_]");

        public static bool IsValidDoEmail(string strEmail)

        /// 檢測是否是正确的Url

        /// <param name="strUrl">要驗證的Url</param>

        public static bool IsURL(string strUrl)

            return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");

        /// 判斷是否為base64字元串

        public static bool IsBase64String(string str)

            //A-Z, a-z, 0-9, +, /, =

            return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");

        /// 檢測是否有Sql危險字元

        /// <param name="str">要判斷字元串</param>

        public static bool IsSafeSqlString(string str)

            return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");

}

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366618,如需轉載請自行聯系原作者