gpt4 book ai didi

c# - 仅当逗号在双引号之外时才按逗号拆分字符串#

转载 作者:太空宇宙 更新时间:2023-11-03 18:29:37 25 4
gpt4 key购买 nike

"TIMESTAMP (UTC)","LOG TYPE","DEVICE TYPE","DEVICE","MESSAGE","PARAMETERS"
"2014-08-12 17:30:34.437","Warning","DiverGate","141403G00294","Diver gate(s) did not connect since","2014-08-08 06:37:31 (UTC)"
"2014-08-12 17:30:34.577","Warning","DiverGate","141403G00120","Diver gate(s) did not connect since","2014-08-08 06:46:22 (UTC)"
"2014-08-13 06:45:18.890","Error","DiverGate","141403G00294","Was set to inactive, because it did not connect since","2014-08-08 06:37:31 (UTC)"
"2014-08-13 07:00:18.903","Error","DiverGate","141403G00120","Was set to inactive, because it did not connect since","2014-08-08 06:46:22 (UTC)"

这是我的 .csv 文件,我需要从文件中读取信息,但我需要用双引号外的逗号分隔信息,因为在其他一些文件中,我可以在某些信息中找到逗号,尤其是在消息、日志中类型,...

 string url = @"E:\Project.csv";
Stream stream = File.Open(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
string[] lines = null;

using (StreamReader sr = new StreamReader(stream))
{
string str = sr.ReadToEnd();
lines = Regex.Split(str, //what expression is going here);
}

最佳答案

你可以试试Lookaround

They do not consume characters in the string, but only assert whether a match is possible or not.

(?<="),(?=")

这里是 online demo并在 regexstorm 进行了测试

模式解释很简单

  (?<=                     look behind to see if there is:
" '"'
) end of look-behind
, ','
(?= look ahead to see if there is:
" '"'
) end of look-ahead

关于c# - 仅当逗号在双引号之外时才按逗号拆分字符串#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25384056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com