I have a problem with my regexp.
The input to match are "telegrams" of various length.
"Fields" within a telegram are separated by ","
If a "," char is part of a field the field must be inclosed by quotes: (field1,"field2 a,field2b",fiel d3,...)
The problem with the regexp I'm using is that it does not parse "empty fields" correct.
This telegram will return 6 matches:
"04",N,1,2, 3,4
This telegram will return 6 matches:
"04",N,1,2, ,4
But this telegram will only return 5 matches:
"04",N,1,,, 4
I have very little experience of regexp, please help.
//Fredrik.
The input to match are "telegrams" of various length.
"Fields" within a telegram are separated by ","
If a "," char is part of a field the field must be inclosed by quotes: (field1,"field2 a,field2b",fiel d3,...)
The problem with the regexp I'm using is that it does not parse "empty fields" correct.
This telegram will return 6 matches:
"04",N,1,2, 3,4
This telegram will return 6 matches:
"04",N,1,2, ,4
But this telegram will only return 5 matches:
"04",N,1,,, 4
Code:
private static MatchCollection doMatch(string telegramString)
{
string pattern = "(\".+?\"|[^,]+|,,)";
MatchCollection col = Regex.Matches(telegramString, pattern);
return col;
}
//Fredrik.
Comment