求一正则,只保留数字和减号和小数点 小数点保留条件->只有在数字之间的小数点才会被保留减号保留条件->只有在数字前一位是减号的时候才会被保留例如 ~@-$.%-4.7%5正则后的结果为 -4.75 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string str = "~@-$.%-4.7%5"; string result = Regex.Replace(str, @"[^\d-.]|[-.](?!\d)", ""); Console.WriteLine(result);//-4.75 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(); 有什么好的集合类,可以处理 string->string的? 两个函数间值传递 socket 控件数组 急! 急。。。增加记录如何清空界面 不小心删除东西了,“剪贴板循环” 如何重绘Graphics画的东西? WPF中使用webBrowser控件不能播放Flash 请问,如何用.Net实现枚举局域网中所有的SQL实例。 请问怎样去掉空格? 路径限制问题 大家过来看看咯。 給DataGridView家行標題的問題(HeaderCell)
string str = "~@-$.%-4.7%5";
string result = Regex.Replace(str, @"[^\d-.]|[-.](?!\d)", "");
Console.WriteLine(result);
//-4.75
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();