小数点保留条件->只有在数字之间的小数点才会被保留
减号保留条件->只有在数字前一位是减号的时候才会被保留
例如 ~@-$.%-4.7%5
正则后的结果为 -4.75

解决方案 »

  1.   


                string str = "~@-$.%-4.7%5";
                string result = Regex.Replace(str, @"[^\d-.]|[-.](?!\d)", "");
                Console.WriteLine(result);
    //-4.75
      

  2.   


                string strRef =@"\-?\d+\.+\d+";//正则 \-?表示-号存在或者不存在 \d+代表数字1或多个 \.+后面同理。
                MatchCollection matches = Regex.Matches(inputs, strRef, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                strRef = string.Empty;
                foreach (Match match in matches)
                {
                   Console.WriteLine( match.Groups[0].Value);
                }
                Console.WriteLine("______________________________");
                Console.ReadKey();