c# - Fixing badly formatted string with number and thousands seperator
问题描述
I am receiving a string with numbers, nulls, and delimiters that are the same as characters in the numbers. Also there are quotes around numbers that contain a comma(s). With C#, I want to parse out the string, such that I have a nice, pipe delimited series of numbers, no commas, 2 decimal places.
I tried the standard replace, removing certain string patterns to clean it up but I can't hit every case. I've removed the quotes first, but then I get extra numbers as the thousands separator turns into a delimiter. I attempted to use Regex.Replace
with wildcards but can't get anything out of it due to the multiple numbers with quotes and commas inside the quotes.
edit for Silvermind: temp = Regex.Replace(temp, "(?:\",.*\")","($1 = .\n)");
I don't have control over the file I receive. I can get most of the data cleaned up. It's when the string looks like the following, that there is a problem:
703.36,751.36,"1,788.36",887.37,891.37,"1,850.37",843.37,"1,549,797.36",818.36,749.36,705.36,0.00,"18,979.70",934.37
Should I look for the quote character, find the next quote character, remove commas from everything between those 2 chars, and move on? This is where I'm headed but there has to be something more elegant out there (yes - I don't program in C# that often - I'm a DBA).
I would like to see the thousands separator removed, and no quotes.
解决方案
This regex pattern will match all of the individual numbers in your string:
(".*?")|(\d+(.\d+)?)
(".*?")
matches things like"123.45"
(\d+(.\d+)?)
matches things like123.45
or123
From there, you can do a simple search and replace on each match to get a "clean" number.
Full code:
var s = "703.36,751.36,\"1,788.36\",887.37,891.37,\"1,850.37\",843.37,\"1,549,797.36\",818.36,749.36,705.36,0.00,\"18,979.70\",934.37";
Regex r = new Regex("(\".*?\")|(\\d+(.\\d+)?)");
List<double> results = new List<double>();
foreach (Match m in r.Matches(s))
{
string cleanNumber = m.Value.Replace("\"", "");
results.Add(double.Parse(cleanNumber));
}
Console.WriteLine(string.Join(", ", results));
Output:
703.36, 751.36, 1788.36, 887.37, 891.37, 1850.37, 843.37, 1549797.36, 818.36, 749.36, 705.36, 0, 18979.7, 934.37
推荐阅读
- python - 根据特定条件将一串单词分成字符
- cordova - Cordova Mobile 应用程序的离线数据缓存
- c++ - C++中高效的大稀疏块压缩线性方程
- java - 合并 Firestore 查询时出现重复数据
- julia - 性能成本:没有跨功能障碍的循环融合
- java - 如何在 springMVC 中为每个控制器设置默认 url 或 requestMapping
- sqlalchemy - 身份验证错误 Airflow 1.9.0 尝试登录 - 无效的盐
- docker - 使用 Dockerfile 在 Jenkins 从站中安装打包程序
- c - CAS for c、openmp的实现
- python - 无法从 Python 连接到 ODBC 错误:找不到文件 (0) (SQLDriverConnect)")