C# 使用c在双引号中拆分值#

C# 使用c在双引号中拆分值#,c#,C#,我有一个文本文件,其值如下 "444","/5"," ", " "," john""66""88" 我只想得到带双引号的值 所以我想要七个字符串,如下所示 first = "444" second = "/5" third = " " fourth =" " fifth = "john" sixth= "66" seven= "88" .Split(“”)似乎不起作用使用正则表达式而不是拆分,如果出于某种原因,引用的文本中有一个逗号,并且您按逗号拆分,那么您的数据将被损坏 使用以下命令:

我有一个文本文件,其值如下

"444","/5"," ", " "," john""66""88"
我只想得到带双引号的值 所以我想要七个字符串,如下所示

first = "444"
second = "/5"
third = " "
fourth =" "
fifth  = "john"
sixth= "66"
seven= "88"

.Split(“”)
似乎不起作用

使用正则表达式而不是拆分,如果出于某种原因,引用的文本中有一个逗号,并且您按逗号拆分,那么您的数据将被损坏

使用以下命令:

Regex reg = new Regex("\"([^\"]*?)\"");

List<string> elements = new List<string>();

var matches = reg.Matches(theExpression);

foreach(Match match in matches)
{
    var theData = match.Groups[1].Value;
    elements.Add(theData);
}

//Now you have in elements a list with all 
//the values from the string properly splitted.
Regex reg=newregex(“\”([^\“]*?)\”);
列表元素=新列表();
var matches=注册匹配(表达式);
foreach(匹配中的匹配)
{
var theData=match.Groups[1]。值;
元素。添加(数据);
}
//现在您已经在一个包含所有元素的列表中找到了元素
//字符串中的值已正确拆分。
编辑 我刚刚发现,您的部分可能不是逗号分隔的。下面是一种基于字符的方法:

string s = "\"444\",\"/5\",\" \", \" \",\" john\"\"66\"\"88\"";

var myParts = new List<string>();
int pos1 = s.IndexOf('"');
if (pos1 == -1)
    throw new Exception("No qoutes!");
int pos2 = s.IndexOf('"', pos1 + 1);
if (pos1 == -1)
    throw new Exception("No second quote!");

while (true) {
    myParts.Add(s.Substring(pos1, pos2 - pos1 + 1));
    pos1 = s.IndexOf('"', pos2 + 1);
    if (pos1 == -1)
        break;
    pos2 = s.IndexOf('"', pos1 + 1);
    if (pos2 == -1)
        throw new Exception("No final closing quote!");
};

有什么办法解决你的问题吗?你不是在用逗号而不是空格分割吗?。使用这样的正则表达式:“\”([^\\”]*?)\“不,这就是区别所在,我需要双代号的值。我是c夏普的新手,请提供完整的正则表达式,这将有助于我编辑我的问题,以显示我需要什么,逗号并不重要,我只需要两个代码,我发布的代码将返回您想要的,您尝试过吗?我对这个例子做了一些扩展。另外,逗号也很重要,如果每个例子都有“Doe,John”作为值,用逗号拆分会把它一分为二。
string s = "\"444\",\"/5\",\" \", \" \",\" john\"\"66\"\"88\"";
//Take away the leading and the final ", then split with the full "," -> These are your parts 
var parts = s.Trim().Substring(1,s.Length-2).Split(new string[] { "\",\"" }, StringSplitOptions.None);
//Now get them back as List, each wrapped in "part"
var qoutedParts = parts.Select(p => "\"" + p + "\"").ToList();