我如何在Visual Basic中将逗号替换为点
如果,"
和",
之间的comma
替换为dot
例如,在第一行中将"482,5"
替换为"482.5"
"Peter",1,1,1,1,500,"500",631,"631",19,"482,5",1
"Peter",1,1,1,1,500,"500",631,"631",19,"482,5",2
"Peter",1,1,1,1,1984,"1984",635,"635",4,"101,5",3
"Peter",1,1,1,1,500,"500",2000,"2000",19,"482,5",4
"Peter",1,1,1,1,500,"500",1962,"1962",18,"457",5
"Peter",1,1,1,1,486,"486",613,"613",18,"457",6
"Peter",1,1,1,1,1016,"1016",322,"322",19,"482,5",7
"Peter",1,1,1,1,933,"933",444,"444",16,"406,5",8
"Peter",1,1,1,1,250,"250",476,"476",16,"406,5",9
"Peter",1,1,1,1,250,"250",476,"476",16,"406,5",10
"Peter",1,1,1,1,234,"234",933,"933",16,"406,5",11
"Peter",1,1,1,1,250,"250",965,"965",16,"406,5",12
答案 0 :(得分:1)
一般情况下,我建议使用CSV解析器解析csv,因为CSV格式比看起来更复杂。有关详细信息,请参阅RFC 4180。理想的解决方案是识别有问题的列,然后仅替换这些列中的文本。
正则表达式方法必须做出一些假设。即正则表达式方法在某些情况下会起作用,而在其他情况下则不起作用。
可能有些人可以编写一个非常先进的正则表达式来正确处理csvs。但它们难以理解且难以维护。我们在这里做一些假设:
"
。即没有'
-s。"asd""ghi"
。这是一个更令人困惑的例子:"asd"","
。所以正则表达式是:
(?:^|,)"[^",]*,
替换为:$1.
说明:
(?:...)
是一个非捕获组(?:^|,)
匹配行首或逗号"
以匹配起始引用[^",]*
匹配的所有内容既不是引号也不是逗号。因此,它可以防止通过多个字段进行匹配。,
(...)
捕获内部的东西。即逗号前的一切。$1
中指的是被捕获的组。即替换是匹配的东西,然后是一个点。结束逗号不在小组中,所以这就是替换的方式。