使用数字和千位分隔符修复格式错误的字符串

我收到一个字符串,其中包含与数字中的字符相同的数字、空值和分隔符。也有包含逗号的数字引号。使用 C#,我想解析字符串,这样我就有了一个很好的、管道分隔的数字系列,没有逗号,2 个小数位。

我尝试了标准替换,删除了某些字符串模式来清理它,但我不能满足所有情况。我首先删除了引号,但是当千位分隔符变成分隔符时,我得到了额外的数字。我尝试使用Regex.Replace通配符,但由于引号中有多个带有引号和逗号的数字,因此无法从中得到任何东西。

为 Silvermind 编辑: temp = Regex.Replace(temp, "(?:\" , .*\")","($1 = . \n)");

我无法控制收到的文件。我可以清理大部分数据。当字符串如下所示时,就会出现问题:

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

我应该寻找引号字符,找到下一个引号字符,从这两个字符之间的所有内容中删除逗号,然后继续吗?这是我要去的地方,但那里必须有更优雅的东西(是的 - 我不经常用 C# 编程 - 我是一名 DBA)。

我希望看到删除了千位分隔符,并且没有引号。


繁花如伊
浏览 184回答 2
2回答

守着星空守着你

此正则表达式模式将匹配字符串中的所有单个数字:(".*?")|(\d+(.\d+)?)(".*?")匹配之类的东西&nbsp;"123.45"(\d+(.\d+)?)匹配诸如123.45或之类的东西123从那里,您可以对每个匹配项进行简单的搜索和替换,以获得“干净”的数字。完整代码:&nbsp; 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";&nbsp; Regex r = new Regex("(\".*?\")|(\\d+(.\\d+)?)");&nbsp; List<double> results = new List<double>();&nbsp; foreach (Match m in r.Matches(s))&nbsp; {&nbsp; &nbsp; string cleanNumber = m.Value.Replace("\"", "");&nbsp; &nbsp; results.Add(double.Parse(cleanNumber));&nbsp; }&nbsp; Console.WriteLine(string.Join(", ", results));输出: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

温温酱

使用跟踪状态的解析器类型的解决方案来解决这个问题会更简单。正则表达式适用于常规文本,只要您有上下文,就很难用正则表达式解决。像这样的东西会起作用。internal class Program{&nbsp; &nbsp; private static string testString = "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";&nbsp; &nbsp; private static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; bool inQuote = false;&nbsp; &nbsp; &nbsp; &nbsp; List<string> numbersStr = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; int StartPos = 0;&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder SB = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; for(int x = 0; x < testString.Length; x++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(testString[x] == '"')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inQuote = !inQuote;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(testString[x] == ',' && !inQuote )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbersStr.Add(SB.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SB.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(char.IsDigit(testString[x]) || testString[x] == '.')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SB.Append(testString[x]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(SB.Length != 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbersStr.Add(SB.ToString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var nums = numbersStr.Select(x => double.Parse(x));&nbsp; &nbsp; &nbsp; &nbsp; foreach(var num in nums)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(num);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP