K
Kristopher K. Kruger
I was hoping to parse a csv file with a regular expression, the
following is what i have:
string input = "8/5/2004,WITHDRAWAL,,\"($2,000.00)\",";
string pattern = "(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)";
Regex regex = new Regex(pattern);
string[] sa = regex.Split(input);
string all = "";
int count = 0;
foreach (string s in sa)
{all += count.ToString() + ".\t" + s + "\n\r";count++;}
MessageBox.Show(all);
That code works fine except I return empty blocks where there were not
any in the source.
For instance, I get the following:
0. <blank>
1. 8/5/2004
2. <blank>
3. WITHDRAWAL
4. <blank>
5. <blank>
6. <blank>
7. "($2,000.00)"
8. <blank>
9. <blank>
10. <blank>
There should be only the one blank, the one appearing between WITHDRAWAL
and the dollar amount, where there were back-to-back commas in the source.
What I would want back instead:
0. 8/5/2004
1. WITHDRAWAL
2. <blank>
3. "($2,000.00)"
4. <blank>
(though the 4. <blank> is optional as far as I am concerned since it is
at the end)
Thanks.
following is what i have:
string input = "8/5/2004,WITHDRAWAL,,\"($2,000.00)\",";
string pattern = "(?:^|,)(\"(?:[^\"]+|\"\")*\"|[^,]*)";
Regex regex = new Regex(pattern);
string[] sa = regex.Split(input);
string all = "";
int count = 0;
foreach (string s in sa)
{all += count.ToString() + ".\t" + s + "\n\r";count++;}
MessageBox.Show(all);
That code works fine except I return empty blocks where there were not
any in the source.
For instance, I get the following:
0. <blank>
1. 8/5/2004
2. <blank>
3. WITHDRAWAL
4. <blank>
5. <blank>
6. <blank>
7. "($2,000.00)"
8. <blank>
9. <blank>
10. <blank>
There should be only the one blank, the one appearing between WITHDRAWAL
and the dollar amount, where there were back-to-back commas in the source.
What I would want back instead:
0. 8/5/2004
1. WITHDRAWAL
2. <blank>
3. "($2,000.00)"
4. <blank>
(though the 4. <blank> is optional as far as I am concerned since it is
at the end)
Thanks.