C#正则法只取数字和“-”实现方法

            //834858dsdf-12-34513-99
            string s1 = "";
            string msg = "834858dsdf-12-34513-99";
            Regex regex = new Regex(@"\d+", RegexOptions.ECMAScript);
            Match match = regex.Match(msg);
            while (match.Value.Length != 0)
            {
                s1 = Join(match.Value, -);
                match = regex.Match(msg, match.Index + match.Value.Length);
            }
            Console.WriteLine(s1);
            Console.ReadKey();

 static string Join(string[] strs, string seperater)//加上分隔符“-”
        {
            string result = "";
            for (int i = 0; i < strs.Length - 1; i++)
            {
                result = result + strs[i] + seperater;
            }
            if (strs.Length > 0)
            {
                result = result + strs[strs.Length - 1];
            }
            return result;
        }

运行结果:834858-12-34513-99


版权声明:本文为rzqcjwrz818181原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。