I am relatively new with Regular Expressions so please excuse me.
I am currently trying to group each line based on the record line. So, for example, I want all lines proceding the record Line to be grouped into one string, until the next record line. I have been trying to use regular expressions, and I have obtained a result that is very close to what I want, however, there is a newline present at the beginning of the array that I am reading it into.
This is the code I am using to split the data up.
using (StreamReader sr = new StreamReader(file))
{
string line;
line = sr.ReadToEnd();
string[] parts = Regex.Split(line, @"(?=PA11)");
List<string> parameterList = new List<string>(parts);
foreach (string s in parameterList)
{
listBox1.Items.Add(s);
}
}
And this is the result looks like this:
*newline*
LINE 000001 000001 TEST A B TEST OUTPUT *More Lines*
LINE 000002 000002 TEST A B TEST OUTPUT *More Lines*
If anyone can tell me what it is I am doing wrong, I would greatly appreciate it. Thank you in advance.
Looks to me like it’s not inserting a newline but a blank entry. Your regex matches the very beginning of the input because the first line starts with
PA11, and it doesn’t consume any characters, so the first item in thepartsarray is an empty string. You should be able to prevent that by forcing the regex to consume some characters, such as the newline preceding thePA11line:…or by making sure it doesn’t match unless there’s a newline before
PA11: