C# 正規表示式 REGULAR EXPRESSION 判斷字串是否是為16進制字串[只包含0~9 A~F a~f] (GOOGLE: C# 字串 只有數字​)

C# 正規表示式 REGULAR EXPRESSION 判斷字串是否是為16進制字串[只包含0~9 A~F a~f] (GOOGLE: C# 字串 只有數字​)

C# 正規表示式 REGULAR EXPRESSION 判斷字串是否是為16進制字串[只包含0~9 A~F a~f] (GOOGLE: C# 字串 只有數字)


資料來源: https://blog.csdn.net/u013489804/article/details/46472587

        public static void Thread_ImportCardCSV(object arg)
        {
            ProgressDialog d = (ProgressDialog)arg;
            Thread.Sleep(500);

            //---
            //do something
            m_ALImportCard.Clear();

            int intCount = 0;
            m_StrPercentage = "...";
            d.m_strMessage = m_StrMain + "\n" + m_StrPercentage;
            StreamReader SRcount = new StreamReader(m_StrImportCardCSVPath);
            while (!SRcount.EndOfStream)// 每次讀取一行,直到檔尾
            {
                String line = SRcount.ReadLine();// 讀取文字到 line 變數
                intCount++;
                if ((intCount % 3) == 0)
                {
                    m_StrPercentage = "";
                }
                else
                {
                    m_StrPercentage += ".";
                }
                d.m_strMessage = m_StrMain + "\n" + m_StrPercentage;

            }
            SRcount.Close();
            intCount--;

            int intnowindex = 0;
            StreamReader sr = new StreamReader(m_StrImportCardCSVPath);
            String StrTitle = "";
            while (!sr.EndOfStream)// 每次讀取一行,直到檔尾
            {
                String line = sr.ReadLine();// 讀取文字到 line 變數
                if (intnowindex == 0)
                {
                    StrTitle = line;
                }
                else
                {
                    m_StrPercentage = String.Format("{0}/{1}", intnowindex-1, intCount);
                    d.m_strMessage = m_StrMain + "\n" + m_StrPercentage;
                    bool blncheck = true;
                    string[] strs = line.Split(',');
                    //string pattern = @"^[a-zA-Z0-9]\d{2}$"; //开头以字母或数字,然后后面加两个数字字符
                    //string pattern = @"^[a-zA-Z0-9]*$"; //匹配所有字符都在字母和数字之间
                    //string pattern = @"^[a-z0-9]*$"; //匹配所有字符都在小写字母和数字之间
                    //string pattern = @"^[A-Z][0-9]*$"; //以大写字母开头,后面的都是数字
                    //string pattern = @"^\d{3}-\d{2}$";//匹配 333-22 格式,三个数字加-加两个数字
                    string pattern = @"^[a-fA-F0-9]*$"; //匹配所有字符都在16進制
                    if (strs[0].Length!=16)
                    {
                        blncheck = false;
                    }
                    else
                    {
                        if (!System.Text.RegularExpressions.Regex.IsMatch(strs[0], pattern))
                        {
                            blncheck = false;
                        }
                    }
                    if ((StrTitle.Length > 0) && (line.Length >= 16) && (blncheck))
                    {
                        m_ALImportCard.Add(line);
                    }
                }
                intnowindex++;
            }
            sr.Close();
            //---do something

            d.Invoke(new Action(d.Close));
        }


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *