C# C Visual Studio中csv到2D数组的验证和解析
只是尝试用C制作一个加载csv文件的按钮,验证并解析它们:C# C Visual Studio中csv到2D数组的验证和解析,c#,regex,csv,C#,Regex,Csv,只是尝试用C制作一个加载csv文件的按钮,验证并解析它们: protected void Upload_Btn_Click(object sender, EventArgs e) { string test = PNLdataLoader.FileName; //checks if file is csv Regex regex = new Regex("*.csv"); Match match = regex.Match(test); if (match
protected void Upload_Btn_Click(object sender, EventArgs e)
{
string test = PNLdataLoader.FileName;
//checks if file is csv
Regex regex = new Regex("*.csv");
Match match = regex.Match(test);
if (match.Success)
{
string CSVFileAsString = System.Text.Encoding.ASCII.GetString(PNLdataLoader.FileBytes);
System.IO.MemoryStream MS = new System.IO.MemoryStream(PNLdataLoader.FileBytes);
System.IO.StreamReader SR = new System.IO.StreamReader(MS);
//Store each line in CSVlines array of strings
string[] CSVLines = new string[0];
while (!SR.EndOfStream)
{
System.Array.Resize(ref CSVLines, CSVLines.Length + 1);
CSVLines[CSVLines.Length - 1] = SR.ReadLine();
}
}
到目前为止,我用它将行存储在CSVLines中,但我不确定正则表达式出了什么问题。有没有更有效的方法来做到这一点?这不是一个有效的表达式,它说匹配*0或更多次之前的任何字符,因为之前没有字符,这是一个问题 这可能会匹配大多数东西,它不包括特殊字符
Regex regex = new Regex("[a-zA-Z0-9]{1,}.csv");
您也可以这样做:
if(test.EndsWith(".csv"))
最后,我想把你的数组改成一个列表或类似的东西,下面进一步解释:
编辑:
列表在System.Collections.Generic中我没有看到二维数组,我错过了什么吗?@MarkF我还没有找到那个部分,但仍在查看有关如何实现它的文档。
//Store each line in CSVlines array of strings
List<string> CSVLines = new List<string>();
while (!SR.EndOfStream)
{
CSVLines.Add(SR.ReadLine());
}