String compare, sort, regex split I am writing simple code to accept/read a input of lines from a text file, split it into my class variables, sort them and finally display the input in the ordered form. But I have been struggling for the past couple of days to split the read line in appropriate manner.

static void Main(string[] args)
{
    List&lt;MyClass&gt; myClassList = new List&lt;MyClass&gt;();

    string strpath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\input.txt";
    if (File.Exists(strpath))
    {
        using (var sr = new StreamReader(strpath))
        {
            string[] strFirst, strSecond;
            while (sr.Peek() &gt;= 0)
            {
                strFirst = Regex.Split(sr.ReadLine().Replace(" ", ""), "([0-9]+)");
                strFirst = Regex.Split(sr.ReadLine().Replace("|", "&gt;&gt;"), "([0-9]+)");

                myClassList.Add(new MyClass
                {
                   Number = strFirst[1], Name = strFirst[2], Separator = strFirst[1].Split("&gt;&gt;")}
                });

            }                
        }
    }        

    myList = GetResults(myClassList);

    foreach (MyClass myclass in myList)
        {               
            Console.WriteLine(myclass[i].Number + myclass[i].Name + myclass[i].Separator);
            Console.WriteLine(myclass.Name);
        }

        Console.ReadLine();
    }


Sample input line from text file, which I want to process:

var input = "5 string one | 5.2 string two | 5.2.1 string three\r\n" +
                    "5 string four &gt;&gt; 5.6 string five &gt;&gt; string six\r\n" +
                    "1 string seven | 1.1 string eight | 1.1.1 string nine\r\n";


Output:




MyClass
Number[0] = 5
Name[0] = string one
Separator[0] = |

Number[1] = 5.2
Name[1] = string two
Separator[1] = |

Number[2] = 5.2.1
Name[2] = string three
Separator[2] =
Try this:

class Program
{
    static void Main(string[] args)
    {
        //var input = File.ReadAllText("input.txt");

        var input = "5 string one | 5.2 string two | 5.2.1 string three\r\n"+
                    "5 string four &gt;&gt; 5.6 string five &gt;&gt; string six\r\n" +
                    "1 string seven &gt;&gt; 1.1 string eight || 1.1.1 string nine\r\n";

        var matches = Regex.Matches(input, @"([0-9. ]*)(string \w*)([\|&gt; $]*)",
            RegexOptions.Multiline);
        var template = "Number[{0}] = {1}\r\n" +
                       "Name[{0}] = {2}\r\n" +
                       "Separator[{0}] = {3}\r\n";
        var sb = new StringBuilder();
        sb.AppendLine("MyClass");
        int counter = 0;
        foreach (Match item in matches)
        {
            sb.AppendLine(String.Format(template,
                counter++,
                item.Groups[1].ToString().Trim(),
                item.Groups[2].ToString().Trim(),
                item.Groups[3].ToString().Trim()));
        }
        Console.WriteLine(sb.ToString());
    }
}


The output is:

MyClass
Number[0] = 5
Name[0] = string one
Separator[0] = |

Number[1] = 5.2
Name[1] = string two
Separator[1] = |

Number[2] = 5.2.1
Name[2] = string three
Separator[2] =

Number[3] = 5
Name[3] = string four
Separator[3] = &gt;&gt;

Number[4] = 5.6
Name[4] = string five
Separator[4] = &gt;&gt;

Number[5] =
Name[5] = string six
Separator[5] =

Number[6] = 1
Name[6] = string seven
Separator[6] = &gt;&gt;

Number[7] = 1.1
Name[7] = string eight
Separator[7] = ||

Number[8] = 1.1.1
Name[8] = string nine
Separator[8] =


Based on the request in your comment, I'm adding ordering of the output. Just replace the foreach statement above  with the following lines and make sure you add a using System.Dynamic; to the top of your class file:

List&lt;dynamic&gt; items = new List&lt;dynamic&gt;();
foreach (Match match in matches)
{
    dynamic item = new ExpandoObject();
    item.Index = counter++;
    item.Number = match.Groups[1].ToString().Trim();
    item.Name = match.Groups[2].ToString().Trim();
    item.Separator = match.Groups[3].ToString().Trim();
    items.Add(item);
}

var sb = new StringBuilder();
sb.AppendLine("MyClass");
foreach (var item in items.OrderBy(i =&gt; i.Number))
{
    sb.AppendLine(String.Format(template,
    item.Index,
    item.Number,
    item.Name,
    item.Separator));
}