简体   繁体   English

在C#中,如何在逗号分隔文件中的另一个双引号内删除/替换Double Quote?

[英]In C#, how do I remove/replace the Double Quote inside another double quote within a comma delimited file?

How do I remove or replace the Double Quote inside another double quote within a comma delimited file? 如何在逗号分隔文件中的另一个双引号内删除或替换Double Quote?

Following is the Text File content: 以下是文本文件内容:

,"188 "F" ST #1",

I need to take off the double quote as below: 我需要取下双引号如下:

,"188 F ST #1",

I've been trying regex and other replace functions but cannot figure this out. 我一直在尝试正则表达式和其他替换函数,但无法弄清楚这一点。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks 谢谢

This problem is inherently under-specified, because it does not offer any way to distinguish quotes inside the quoted string from the quote that closes up the string. 这个问题本质上是指定不足的,因为它没有提供任何方法来区分引用字符串中的引号和关闭字符串的引号。

One way to define this would be "all quotes not preceded and not followed by a comma", thus excluding ," and ", sequences from consideration; 定义此方法的一种方法是“所有引号都不在前面,而不是后跟逗号”,从而排除,"",来自考虑的序列; you also need to preserve initial and trailing quotes. 您还需要保留初始和尾随引号。

Here is a regex to do it: 这是一个正则表达式:

(?<!^|,)\"(?!$|,)

The quote character \\" in the middle of the expression is surrounded by a negative look-behind on the left and a negative look-ahead on the right. The look-behind rejects quotes at the beginning of line and quotes that follow a comma. The look-ahead rejects quotes at the end of the line, and quotes followed by a comma. 表达式中间的引号字符\\"被左侧的负面后视和右侧的负面前瞻所包围。后方在行的开头拒绝引号,在逗号后面引用引号。前瞻拒绝行尾的引号,引号后跟逗号。

Demo. 演示。

try this 试试这个

string content = "words ,\"188 \"F\" ST #1\", more words";    
content =content.Replace("\"","");
content ="\"" + content.Replace(",","\",\"") + "\"";

This will wrap every comma with double quotes, it will also make sure the string starts and ends with a double quote 这将用双引号包装每个逗号,它还将确保字符串以双引号开头和结尾

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM