2
\$\begingroup\$

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<MyClass> myClassList = new List<MyClass>();

    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() >= 0)
            {
                strFirst = Regex.Split(sr.ReadLine().Replace(" ", ""), "([0-9]+)");
                strFirst = Regex.Split(sr.ReadLine().Replace("|", ">>"), "([0-9]+)");

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

            }                
        }
    }        

    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 >> 5.6 string five >> 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] =
\$\endgroup\$
  • \$\begingroup\$ You probably have a problem in ` strFirst = Regex.Split(sr.ReadLine().Replace(" ", ""), "([0-9]+)"); strFirst = Regex.Split(sr.ReadLine().Replace("|", ">>"), "([0-9]+)");` because the first value or strFirst is never used. \$\endgroup\$ – C4stor Jul 19 '13 at 7:51
  • \$\begingroup\$ @Tim: yes. the sample input lines are one of the three lines in from a file \$\endgroup\$ – DotNetGeek Jul 19 '13 at 7:54
  • \$\begingroup\$ @C4stor: I do have a problem there, but struggling what to write in order to achieve my resultset. Any suggestion \$\endgroup\$ – DotNetGeek Jul 19 '13 at 7:54
  • \$\begingroup\$ moreover when I do char[] delim = {'|','>' }; strFirst = sr.ReadLine().Split(delim); I get strFirst[0] = 5 string one & strFirst[1] = 5.1 string two & strFirst[2] = string three but now i want to further process these indexes into number and strings so that I can store them into my class variable. Hope, it makes sense. \$\endgroup\$ – DotNetGeek Jul 19 '13 at 8:04
1
\$\begingroup\$

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 >> 5.6 string five >> string six\r\n" +
                    "1 string seven >> 1.1 string eight || 1.1.1 string nine\r\n";

        var matches = Regex.Matches(input, @"([0-9. ]*)(string \w*)([\|> $]*)",
            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] = >>

Number[4] = 5.6
Name[4] = string five
Separator[4] = >>

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

Number[6] = 1
Name[6] = string seven
Separator[6] = >>

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<dynamic> items = new List<dynamic>();
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 => i.Number))
{
    sb.AppendLine(String.Format(template,
    item.Index,
    item.Number,
    item.Name,
    item.Separator));
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for your response, but all it does is prints "MyClass" at the end. Any reasons? \$\endgroup\$ – DotNetGeek Jul 19 '13 at 8:48
  • \$\begingroup\$ Maybe your input file is slightly different then what you posted... I'll update my answer by including the input string in the code. \$\endgroup\$ – Alex Filipovici Jul 19 '13 at 8:54
  • \$\begingroup\$ Hi @Alex, could you also update your answer with the result you get? Thanks anyways \$\endgroup\$ – DotNetGeek Jul 19 '13 at 11:06
  • \$\begingroup\$ thanks once again. This goes ok, but I am trying with one more set ok record but does not fit well. Can you try this line and see why is this happening. 4 string 1 string 11 string 111 | 4.6 string 1 string 12 string 121 string 122 | string 1 string 13 string 131 string 132\r\n" the output should be Number[0]=4 Name[0]="string 1 string 11 string 111" separator[0]= , Number[1]=4.6 Name[1]="string 1 string 12 string 121 string 122" separator[1]= |, Number[2]= Name[2]="string 1 string 13 string 131 string 132 separator[2]=" \$\endgroup\$ – DotNetGeek Jul 19 '13 at 13:36
  • \$\begingroup\$ If this goes ok, maybe you should mark my approach as an answer. If you have some other sample data, in a different format, this will modify fundamentally the regular expression, thus, the relevance of the posted answers. You have the option to modify your question and wait for an answer which matches the updated problem. \$\endgroup\$ – Alex Filipovici Jul 19 '13 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy