ove*_*k77 2 c# csv streamreader
我正在使用StreamReader将CSV文件的每一行读取为字符串.当我处理每一行时,我需要删除任何仅由其他数字包围的逗号.
例如,如果字符串是:
"textfield1", "textfield2", "100.00", "1,070.00"
我只需要从整个字符串中取出"1,070.00"的逗号,结果是:
"textfield1", "textfield2", "100.00", "1070.00"
从CSV文件读取的每个字符串可以在字段数,长度等方面有所不同,所以我需要使用一些东西(正则表达式可能?)来查看整个字符串而无需对所有逗号进行硬编码或删除.
这是我一直在尝试的方法:
StreamReader sr = new StreamReader(strInputFile);
string nextLine = sr.ReadLine();
try
{
while ((nextLine = sr.ReadLine()) != null)
{
string rawtext = nextLine.Replace("[0-9]"+","+"[0-9]" , "[0-9]"+"[0-9]");
// ....rest of code
}
}
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为我不明白该怎么做:)
我是C#的新手并且在Regex中缺乏经验,所以希望这相对简单.
听起来你想要使用正则表达式:
string rawtext = Regex.Replace(nextLine, @"(\d),(\d)", "$1$2");
Run Code Online (Sandbox Code Playgroud)
或者这种等效模式:
string rawtext = Regex.Replace(input, @"(?<=\d),(?=\d)", "");
Run Code Online (Sandbox Code Playgroud)