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

原文地址:https://www.cnblogs.com/wisdo/p/5770124.html