Does anyone know how to split this file with regex
1 TESTAAA SERNUM A DESCRIPTION
2 TESTBBB ANOTHR ANOTHER DESCRIPTION
3 TESTXXX BLAHBL
The lenght of each column
{id} {firsttext} {serialhere} {description}
4 22 6 30+
I'm planning to do it with a regex to store all my values in a string[] like this.
using (StreamReader sr = new StreamReader("c:\\file.txt"))
{
string line = string.Empty;
string[] source = null;
while ((line = sr.ReadLine()) != null)
{
source = Regex.Split(line, @"(.{4})(.{22})(.{6})(.+)", RegexOptions.Singleline);
}
}
But I have 2 problems.
- The split creates a 6 elements source[0] = "" and source[5] ="" when as you can see I have only 4 elements(columns) per line.
- In the case of 3rd line which have the 4th column, if I have blank spaces it creates a position for it but if there's no blank spaces this column is missed.
So what would be the best pattern or solution to split with regex or another solution will be aprreciate it!!! I want to split fixed width. Thanks.