C# 正則表達式及返回匹配的所有結果


  C# 正則表達式是在  System.Text.RegularExpressions 空間定義的,其中的 IsMatch() 方法用於返回 bool 類型,表示要搜索的字符串與傳入的正則表達式是否匹配,如果匹配返回 true, 否則返回 false, 如果想要獲取所有的匹配結果,就要調用 Matches,方法,該方法返回 MatchCollection 集合對象,通過遍歷集合,即可獲取所有的匹配結果。

 

    /// <summary>
        /// 獲取正則表達式匹配結果集
        /// </summary>
        /// <param name="value">字符串</param>
       /// <param name="regx">正則表達式</param>
        /// <returns></returns>
        private float[] GetPathPoint(string value, string regx)
        {
            if (string.IsNullOrWhiteSpace(value))
                return null;
            bool isMatch = System.Text.RegularExpressions.Regex.IsMatch(value, regx);
            if (!isMatch)
                return null;
            System.Text.RegularExpressions.MatchCollection matchCol = System.Text.RegularExpressions.Regex.Matches(value, regx);
            float[] result = new float[matchCol.Count];
            if (matchCol.Count > 0)
            {
                for (int i = 0; i < matchCol.Count; i++)
                {
                    result[i] = float.Parse(matchCol[i].Value);
                }
            }
            return result;
        }

   

  參考文章:

 msdn 正則表達式搜索字符串: https://msdn.microsoft.com/zh-cn/library/ms228595.aspx

JavaScript正則表達式在線測試工具:http://tools.jb51.net/regex/javascript

msdn 正則表達式快速參與  https://msdn.microsoft.com/zh-cn/library/az24scfc(v=vs.110).aspx

正則表達式參考:  http://ahkcn.github.io/docs/misc/RegEx-QuickRef.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM