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

来源:互联网 发布:mac破解迅雷离线下载 编辑:程序博客网 时间:2024/06/08 01:00

            //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

0 0