正则表达式删除由双引号划定的 CSV 字段中的双引号
Posted
技术标签:
【中文标题】正则表达式删除由双引号划定的 CSV 字段中的双引号【英文标题】:Regex to remove double-quotes in CSV fields that are delineated by double-quotes 【发布时间】:2015-02-13 19:37:48 【问题描述】:这是针对 VB.NET 项目的。我现有的方法将逗号分隔的文件转换为管道分隔的文件。它有点挑战性,因为一些字段中有逗号,所以这些字段在字段内容周围有双引号。
这是工作代码(感谢 The Blue Dog 对此的一百万研究):
Private Function ConvertCommaSepToPipeSep() As Boolean
Dim line, result As String
Dim pattern As String = ",([^,""]*(?:""[^""]*"")?[^,""]*)(?=,|$)"
Dim replacement As String = "|$1"
Dim rgx As New Regex(pattern)
'Console.WriteLine("Conversion start time: " & DateTime.Now.ToLongTimeString())
Try
Using sw As New StreamWriter("output.csv")
Using sr As New StreamReader("source.csv")
While Not sr.EndOfStream
line = sr.ReadLine
result = rgx.Replace(line, replacement)
sw.WriteLine(result.Replace(Chr(34), ""))
End While
End Using
End Using
Catch ex As Exception
MessageBox.Show("There was a problem converting the file." & vbcrlf & ex.message)
Return False
End Try
'Console.WriteLine("Conversion end time: " & DateTime.Now.ToLongTimeString())
Return True
End Function
然而,我发现有些字段中也有双引号。
这是我正在转换的源文件中的一些示例行。
122749,JOHN DOE,ACS155,7/5/2014,P,SCH/RC Activation Week 2,HRLY,1299577,Scheduler IT,2204,CVISA-Client Activation,1220000,Svcs Clin Implement,34
110310,JANE DOE,ACS150,2/8/2014,P,"Developed Employee Interface""",HRLY,1267305,Project Management - Client Implementation Services,2500,PJM -Project Management,1410000,Tech Services Development,8
110310,MARY DOE,ACS160,2/8/2014,P,EDManage+ CSV data extract,HRLY,1527401,Project Management - Client Implementation Services,2500,PJM -Project Management,1410000,Tech Services Development,8
129084,ROBERT SMITH,ACS80,9/27/2014,P,,PTO,0,Company General Services,1030,"Time Off - PTO, Holiday, Personal Holiday, FTO",1100000,Client Services Technical,40
117592,HARRY JOHNSON,ACS64,5/10/2014,P,"helped penny post AP ""E"" cks",HRLY,1554404,General Financials IT,2120,CCON-Client Conference Call,1100000,Client Services Technical,1.5
110310,MARK WILSON,ACS130,2/8/2014,P,"""Charge Vs Payment""",HRLY,1267305,Project Management - Clinical Implementation Services,2500,PJM -Project Management,1410000,Tech Services Development,8
这些相同的行需要转换成如下所示:
122749|JOHN DOE|ACS155|7/5/2014|P|SCH/RC Activation Week 2|HRLY|1299577|Scheduler IT|2204|CVISA-Client Activation|1220000|Svcs Clin Implement|34
110310|JANE DOE|ACS150|2/8/2014|P|Developed Employee Interface""|HRLY|1267305|Project Management - Client Implementation Services|2500|PJM -Project Management|1410000|Tech Services Development|8
110310|MARY DOE|ACS160|2/8/2014|P|EDManage+ CSV data extract|HRLY|1527401|Project Management - Client Implementation Services|2500|PJM -Project Management|1410000|Tech Services Development|8
129084|ROBERT SMITH|ACS80|9/27/2014|P||PTO|0|Company General Services|1030|Time Off - PTO, Holiday, Personal Holiday, FTO|1100000|Client Services Technical|40
117592|HARRY JOHNSON|ACS64|5/10/2014|P|helped penny post AP E cks|HRLY|1554404|General Financials IT|2120|CCON-Client Conference Call|1100000|Client Services Technical|1.5
110310|MARK WILSON|ACS130|2/8/2014|P|Charge Vs Payment|HRLY|1267305|Project Management - Clinical Implementation Services|2500|PJM -Project Management|1410000|Tech Services Development|8
在此 CSV 中,文本中包含逗号的列在该列周围加上双引号,上面的正则表达式说明了这一点。但我发现有些字段中也有双引号。可以删除字段中的任何双引号实例,但在某些情况下,该字段可以以双引号结尾或开头,从而产生三个双引号,但我不能只删除所有双引号,因为它们有助于描绘包含逗号的字段在哪里开始和结束。
需要在正则表达式中添加什么来做到这一点?
【问题讨论】:
因为它们应该始终保持平衡,所以将""
什么都不替换怎么样?这将留下一个空的、未加引号的字段或带有单个集合或引号的字段。
""
应该被转换为单个"
。您确定要完全删除它们吗?
@AlexK。是的,你是对的,它们应该是平衡的。并扫描文件,看起来就是这样。那么如何将它添加到现有的正则表达式中呢?
你不能在运行 RE 之前先 csvString = csvString.Replace( ... )
@AlexK。我一直在研究这个,我错过了明显的。做到了。谢谢
【参考方案1】:
""
应该被转换为单个"
。您确定要完全删除它们吗?
– nhahtdh
你不能在运行 RE 之前直接 csvString = csvString.Replace( ... )
——亚历克斯 K。
【讨论】:
以上是关于正则表达式删除由双引号划定的 CSV 字段中的双引号的主要内容,如果未能解决你的问题,请参考以下文章