J
Jeremy
I created a regular expression to parse a line in a csv file;
(\"(?<word>[^\"]+|\"\")*\"|(?<word>[^,]*))
It is capable of taking a line such as field1,field2,field
3,123.12,"1,234.56" and matching each value between the commas into the word
group, so I get
field1
field2
field 3
123.12
1,234.56
My problem is that if I perform a split, or match on a string like
1,1,"123.345" I will get 6 matches back instead of 3.
const string strDelimiter =
"(\\\"(?<word>[^\\\"]+|\\\"\\\")*\\\"|(?<word>[^,]*))";
string strText = "1,1,\"12,212.43\"";
string[] strParts = System.Text.RegularExpressions.Regex.Split(strText ,
strDelimiter,System.Text.RegularExpressions.RegexOptions.Compiled |
System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
System.Text.RegularExpressions.MatchCollection pMatches =
System.Text.RegularExpressions.Regex.Matches(strText ,
strDelimiter,System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
Split returns 13 values, as shown below, and Matches returns 6 items. How
can I just extract the 3 items?
strParts {Dimensions:[13]} string[]
[0] "" string
[1] "1" string
[2] "" string
[3] "" string
[4] "," string
[5] "1" string
[6] "" string
[7] "" string
[8] "," string
[9] "30,478.50" string
[10] "" string
[11] "" string
[12] "" string
(\"(?<word>[^\"]+|\"\")*\"|(?<word>[^,]*))
It is capable of taking a line such as field1,field2,field
3,123.12,"1,234.56" and matching each value between the commas into the word
group, so I get
field1
field2
field 3
123.12
1,234.56
My problem is that if I perform a split, or match on a string like
1,1,"123.345" I will get 6 matches back instead of 3.
const string strDelimiter =
"(\\\"(?<word>[^\\\"]+|\\\"\\\")*\\\"|(?<word>[^,]*))";
string strText = "1,1,\"12,212.43\"";
string[] strParts = System.Text.RegularExpressions.Regex.Split(strText ,
strDelimiter,System.Text.RegularExpressions.RegexOptions.Compiled |
System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
System.Text.RegularExpressions.MatchCollection pMatches =
System.Text.RegularExpressions.Regex.Matches(strText ,
strDelimiter,System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
Split returns 13 values, as shown below, and Matches returns 6 items. How
can I just extract the 3 items?
strParts {Dimensions:[13]} string[]
[0] "" string
[1] "1" string
[2] "" string
[3] "" string
[4] "," string
[5] "1" string
[6] "" string
[7] "" string
[8] "," string
[9] "30,478.50" string
[10] "" string
[11] "" string
[12] "" string