asp.Net(C#)常用正規表現パッケージ

29298 ワード

using System;  
using System.Collections;  
using System.Text.RegularExpressions;  

namespace MSCL  
{  
    //// <summary>  
    /// RegularMatch       
    /// </summary>      
    public class RegHelper  
    {  
        private string _String;  
        private bool _IsEntirety;  

        /**//// <summary>  
        ///            
        /// </summary>  
        public enum Operation  
        {  
            Byte, SByte, Int16, Int32, Int64, Single, Double, Boolean, Char, Decimal, DateTime, Date, Time,  
            EMail, URL, ChinaPhone, ChineseWord, ChinesePostalCode, Number, StringModel_01, StringModel_02, WideWord, NarrowWord, IPAddress,  
            ChineseMobile, ChineseID  
        };  


        #region               (       )
        /// <summary>
        ///               
        /// <code>
        /// MSCL.RegHelper rm = new MSCL.RegHelper();
        /// Response.Write(rm.IsAccordType("123", MSCL.RegHelper.Operation.ChineseWord));
        /// </code>
        /// </summary>
        /// <param name="strVerifyString">String,        </param>
        /// <param name="op">Operation  ,           </param>
        /// <returns></returns>
        public bool IsAccordType(string strVerifyString, Operation op)  
        {  
            return IsAccordType(strVerifyString, op, false);  
        }  
        #endregion  

        #region               (            )  
        /**//// <summary>  
        ///                 
        /// </summary>  
        /// <param name="strVerifyString">String,        </param>  
        /// <param name="op">Operation  ,           </param>  
        /// <param name="IsEntirety">Boolean,               (          )</param>  
        /// <returns></returns>  
        public bool IsAccordType(string strVerifyString, Operation op, bool IsEntirety)  
        {  
            _String = strVerifyString;  
            _IsEntirety = IsEntirety;  

            switch (op)  
            {  
                case Operation.Byte:  
                    {  
                        return IsByte();  
                    }  
                case Operation.SByte:  
                    {  
                        return IsSByte();  
                    }  
                case Operation.Int16:  
                    {  
                        return IsInt16();  
                    }  
                case Operation.Int32:  
                    {  
                        return IsInt32();  
                    }  
                case Operation.Int64:  
                    {  
                        return IsInt64();  
                    }  
                case Operation.Single:  
                    {  
                        return IsSingle();  
                    }  
                case Operation.Double:  
                    {  
                        return IsDouble();  
                    }  
                case Operation.Boolean:  
                    {  
                        return IsBoolean();  
                    }  
                case Operation.Char:  
                    {  
                        return IsChar();  
                    }  
                case Operation.Decimal:  
                    {  
                        return IsDecimal();  
                    }  
                case Operation.DateTime:  
                    {  
                        return IsDateTime();  
                    }  
                case Operation.Date:  
                    {  
                        return IsDate();  
                    }  
                case Operation.Time:  
                    {  
                        return IsTime();  
                    }  
                case Operation.IPAddress:  
                    {  
                        return IsIPAddress();  
                    }  
                case Operation.ChinaPhone:  
                    {  
                        return IsChinaPhone();  
                    }  
                case Operation.ChinesePostalCode:  
                    {  
                        return IsChinesePostalCode();  
                    }  
                case Operation.ChineseMobile:  
                    {  
                        return IsChineseMobile();  
                    }  
                case Operation.EMail:  
                    {  
                        return IsEmail();  
                    }  
                case Operation.URL:  
                    {  
                        return IsURL();  
                    }  
                case Operation.ChineseWord:  
                    {  
                        return IsChineseWord();  
                    }  
                case Operation.Number:  
                    {  
                        return IsNumber();  
                    }  
                case Operation.StringModel_01:  
                    {  
                        return IsStringModel_01();  
                    }  
                case Operation.StringModel_02:  
                    {  
                        return IsStringModel_02();  
                    }  
                case Operation.WideWord:  
                    {  
                        return IsWideWord();  
                    }  
                case Operation.NarrowWord:  
                    {  
                        return IsNarrowWord();  
                    }  
                case Operation.ChineseID:  
                    {  
                        return IsChineseID();  
                    }  
                default:  
                    {  
                        return false;  
                    }  
            }  
        }  
        #endregion  

        #region         

        #region   Byte  (8        ): 0   255           
        /**//// <summary>  
        ///   Byte  (8        ): 0   255           
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsByte()  
        {  
            try  
            {  
                Byte.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   SByte  (8        ): -128   +127        
        /**//// <summary>  
        ///   SByte  (8        ): -128   +127        
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsSByte()  
        {  
            try  
            {  
                SByte.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Int16  (16        ): -32768   +32767           
        /**//// <summary>  
        ///   Int16  (16        ): -32768   +32767           
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsInt16()  
        {  
            try  
            {  
                Int16.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Int32  (32        ):-2,147,483,648   +2,147,483,647           
        /**//// <summary>  
        ///   Int32  (32        ):-2,147,483,648   +2,147,483,647           
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsInt32()  
        {  
            try  
            {  
                Int32.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Int64  (64        ): -9,223,372,036,854,775,808   +9,223,372,036,854,775,807        
        /**//// <summary>  
        ///   Int64  (64        ): -9,223,372,036,854,775,808   +9,223,372,036,854,775,807        
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsInt64()  
        {  
            try  
            {  
                Int64.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Single  (   (32  )    ): -3.402823e38   +3.402823e38        32      
        /**//// <summary>  
        ///   Single  (   (32  )    ): -3.402823e38   +3.402823e38        32      
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsSingle()  
        {  
            try  
            {  
                Single.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Double  (   (64  )    ): -1.79769313486232e308   +1.79769313486232e308        64      
        /**//// <summary>  
        ///   Double  (   (64  )    ): -1.79769313486232e308   +1.79769313486232e308        64      
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsDouble()  
        {  
            try  
            {  
                Double.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Boolean  (   ):true   false  
        /**//// <summary>  
        ///   Double  (   (64  )    ): -1.79769313486232e308   +1.79769313486232e308        64      
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsBoolean()  
        {  
            try  
            {  
                Boolean.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Char  (Unicode(16  )  ):  16                0x0000   0xFFFF  
        /**//// <summary>  
        ///   Char  (Unicode(16  )  ):  16                0x0000   0xFFFF  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsChar()  
        {  
            try  
            {  
                Char.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Char  (96      ):   79,228,162,514,264,337,593,543,950,335    79,228,162,514,264,337,593,543,950,335          
        /**//// <summary>  
        ///   Char  (96      ):   79,228,162,514,264,337,593,543,950,335    79,228,162,514,264,337,593,543,950,335          
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsDecimal()  
        {  
            try  
            {  
                Decimal.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   DateTime  (        ):      (    )0001   1   1     12:00:00     (C.E.) 9999   12   31     11:59:59           
        /**//// <summary>  
        ///   DateTime  (        ):      (    )0001   1   1     12:00:00     (C.E.) 9999   12   31     11:59:59           
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsDateTime()  
        {  
            try  
            {  
                DateTime.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region   Date  (         ):      (    )0001   1   1       (C.E.) 9999   12   31         
        /**//// <summary>  
        ///   Date  (         ):      (    )0001   1   1       (C.E.) 9999   12   31         
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsDate()  
        {  
            DateTime Value;  
            try  
            {  
                Value = DateTime.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            if (Value.Date.ToString() == _String)  
            {  
                return true;  
            }  
            else  
            {  
                return false;  
            }  
        }  
        #endregion  

        #region   Time  (      HHMMSS):      12:00:00     11:59:59        
        /**//// <summary>  
        ///   Time  (      HHMMSS):      12:00:00     11:59:59        
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsTime()  
        {  
            DateTime Value;  
            try  
            {  
                Value = DateTime.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            if (Value.Year == 1 && Value.Month == 1 && Value.Day == 1)  
            {  
                return true;  
            }  
            else  
            {  
                return false;  
            }  
        }  
        #endregion  

        #region   IPAddress  (IPv4                      ,IPv6                  )  
        /**//// <summary>  
        ///   IPAddress  (IPv4                      ,IPv6                  )  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsIPAddress()  
        {  
            try  
            {  
                System.Net.IPAddress.Parse(_String);  
            }  
            catch  
            {  
                return false;  
            }  
            return true;  
        }  
        #endregion  

        #region           (XXX/XXXX-XXXXXXX/XXXXXXXX (\d{3,4})-?\d{7,8}):     (  :3 4 )-(    :7 8 )  
        /**//// <summary>  
        ///           (XXX/XXXX-XXXXXXX/XXXXXXXX (\d{3,4})-?\d{7,8}):     (  :3 4 )-(    :7 8 )  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsChinaPhone()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"(\d{3,4})-?\d{7,8}", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region         (6    \d{6})  
        /**//// <summary>  
        ///         (6    \d{6})  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsChinesePostalCode()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"\d{6}", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region           (13    11    13\d{9})  
        /**//// <summary>  
        ///           (13    11    13\d{9})  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsChineseMobile()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"13\d{9}", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region   EMail  ([email protected] \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)  
        /**//// <summary>  
        ///   EMail  ([email protected] \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsEmail()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region   Internet URL    (http://)  
        /**//// <summary>  
        ///   Internet URL    (http://)  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsURL()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region       ([\u4e00-\u9fa5])  
        /**//// <summary>  
        ///       ([\u4e00-\u9fa5])  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsChineseWord()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"[\u4e00-\u9fa5]", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region      (0 9   [\d]+):     "." "-"  
        /**//// <summary>  
        ///      (0 9   [\d]+):     "." "-"  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsNumber()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"[\d]+", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region        ,      ([\w]+)  
        /**//// <summary>  
        ///        ,      ([\w]+)  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsStringModel_01()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"[\w]+", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region             ([A-Z][a-z]+)  
        /**//// <summary>  
        ///             ([A-Z][a-z]+)  
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsStringModel_02()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"[A-Z][a-z]+", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region       ([^\x00-\xff]):        
        /**//// <summary>  
        ///       ([^\x00-\xff]):        
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsWideWord()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"[^\x00-\xff]", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region       ([\x00-\xff])  
        /**//// <summary>  
        ///       ([^\x00-\xff]):        
        /// </summary>  
        /// <returns>Boolean</returns>  
        protected bool IsNarrowWord()  
        {  
            ArrayList aryResult = new ArrayList();  
            return CommRegularMatch(_String, @"[\x00-\xff]", RegexOptions.None, ref aryResult, _IsEntirety);  
        }  
        #endregion  

        #region               
        protected bool IsChineseID()  
        {  
            if (_String.Length == 15)  
            {  
                _String = CidUpdate(_String);  
            }  
            if (_String.Length == 18)  
            {  
                string strResult = CheckCidInfo(_String);  
                if (strResult == "    " || strResult == "    " || strResult == "    ")  
                {  
                    return false;  
                }  
                else  
                {  
                    return true;  
                }  
            }  
            else  
            {  
                return false;  
            }  
        }  
        #endregion  

        #endregion  

        #region              
        /**//// <summary>  
        ///              
        /// </summary>  
        /// <param name="strVerifyString">String,        </param>  
        /// <param name="strRegular">String,     </param>  
        /// <param name="regOption">RegexOptions,          </param>  
        /// <param name="aryResult">ArrayList,        </param>  
        /// <param name="IsEntirety">Boolean,        </param>  
        /// <returns></returns>  
        public bool CommRegularMatch(string strVerifyString, string strRegular, System.Text.RegularExpressions.RegexOptions regOption, ref System.Collections.ArrayList aryResult, bool IsEntirety)  
        {  
            System.Text.RegularExpressions.Regex r;  
            System.Text.RegularExpressions.Match m;  

            #region              
            if (IsEntirety)  
            {  
                strRegular = strRegular.Insert(0, @"\A");  
                strRegular = strRegular.Insert(strRegular.Length, @"\z");  
            }  
            #endregion  

            try  
            {  
                r = new System.Text.RegularExpressions.Regex(strRegular, regOption);  
            }  
            catch (System.Exception e)  
            {  
                throw (e);  
            }  

            for (m = r.Match(strVerifyString); m.Success; m = m.NextMatch())  
            {  
                aryResult.Add(m);  
            }  

            if (aryResult.Count == 0)  
            {  
                return false;  
            }  
            else  
            {  
                return true;  
            }  
        }  
        #endregion  

        #region            
        private string CheckCidInfo(string cid)  
        {  
            string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "  ", "  ", "  ", "  ", "   ", null, null, null, null, null, "  ", "  ", "   ", null, null, null, null, null, null, null, "  ", "  ", "  ", "  ", "  ", "  ", "  ", null, null, null, "  ", "  ", "  ", "  ", "  ", "  ", null, null, null, "  ", "  ", "  ", "  ", "  ", null, null, null, null, null, null, "  ", "  ", "  ", "  ", "  ", null, null, null, null, null, "  ", null, null, null, null, null, null, null, null, null, "  ", "  ", null, null, null, null, null, null, null, null, "  " };  
            double iSum = 0;  
            string info = string.Empty;  
            System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");  
            System.Text.RegularExpressions.Match mc = rg.Match(cid);  
            if (!mc.Success)  
            {  
                return string.Empty;  
            }  
            cid = cid.ToLower();  
            cid = cid.Replace("x", "a");  
            if (aCity[int.Parse(cid.Substring(0, 2))] == null)  
            {  
                return "    ";  
            }  
            try  
            {  
                DateTime.Parse(cid.Substring(6, 4) + " - " + cid.Substring(10, 2) + " - " + cid.Substring(12, 2));  
            }  
            catch  
            {  
                return "    ";  
            }  
            for (int i = 17; i >= 0; i--)  
            {  
                iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);  
            }  
            if (iSum % 11 != 1)  
            {  
                return ("    ");  
            }  
            else  
            {  
                return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? " " : " "));  
            }  
        }  
        #endregion  

        #region      15   18   
        private string CidUpdate(string ShortCid)  
        {  
            char[] strJiaoYan = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };  
            int[] intQuan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };  
            string strTemp;  
            int intTemp = 0;  

            strTemp = ShortCid.Substring(0, 6) + "19" + ShortCid.Substring(6);  
            for (int i = 0; i <= strTemp.Length - 1; i++)  
            {  
                intTemp += int.Parse(strTemp.Substring(i, 1)) * intQuan[i];  
            }  
            intTemp = intTemp % 11;  
            return strTemp + strJiaoYan[intTemp];  
        }  
        #endregion  
    }  
}