0

I have a problem on my coding. I want to add items to treeview by using regex and c#. But the problem is The nodes that are going to add have the same name.

The code ::

  treeView1.Nodes.Clear();
  string name;
  TreeNode Root = new TreeNode();
  RootNode.Text = "RootNode";
  treeView1.Nodes.Add(RootNode);
  MatchCollection ToGetPulinName = Regex.Matches(richtextbox1.Text,@"pulin (.*?)\{");
  foreach (Match m in ToGetPulinName)
    {
     Group g = m.Groups[1];
     name= g.Value;
     TreeNode SecondRoot = new TreeNode();
     SecondRoot.Text = "PulinRoot:"+pulinname;
     RootNode.Nodes.Add(SecondRoot);
     MatchCollection ToFoundIn = Regex.Matches(codingtxt.Text, @"pulin \w+{(.*?)}", RegexOptions.Singleline);
     foreach (Match min in ToFoundIn)
      {
       Group gMin = min.Groups[1];
       string gStr = gMin.Value;
       string classname;
       MatchCollection classnames = Regex.Matches(codingtxt.Text, @"class (.*?)\{", RegexOptions.IgnoreCase);
       foreach (Match mis in classnames)
        {
         Group gmis = mis.Groups[1];
         classname = gmis.Value;
         TreeNode ClassRoot = new TreeNode();
         ClassRoot.Text = "ClassRoot";
         SecondRoot.Nodes.Add(ClassRoot);
        }
       }
      }

The Result

enter image description here

Please help, Thanks.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Slavi
  • 576
  • 1
  • 3
  • 18

1 Answers1

0

The problem is in how you parse the data using regexs, not the ancillary treeview which is not really an issue here.

Using the below pattern we will search your text and group/extract out all data first into what I am calling namespaces and their associated classes.

In the pattern by using (?< > ) which is a named match capture we can extract the data in an easier way. The following creates a dynamic linq entites which hold our data. I am showing the result of Linqpad's Dump() extension which gives us the data. Also I have added another class to Tester for this example:

var data = @"
pulin Tester  { class Main { } class Omega{} }
pulin Tester2 { }
";

var pattern = @"
pulin\s+                   # Look for our anchor `pulin` which identifies the current namespace,
                           # it will designate each namespace block
(?<Namespace>[^\s{]+)      # Put the namespace name into the named match capture `Namespace`.
    (                      # Now look multiple possible classes
      .+?                  # Ignore all characters until we find `class` text
      (class\s             # Found a class, let us consume it until we find the textual name.
         (?<Class>[^\{]+)  # Insert the class name into the named match capture `Class`.
      )
    )*                     # * means 0-N classes can be found.
                    ";

// We use ignore pattern whitespace to comment the pattern.
// It does not affect the regex parsing of the data.
Regex.Matches(data, pattern, RegexOptions.IgnorePatternWhitespace)
     .OfType<Match>()
     .Select (mt => new
     {
        Namespace = mt.Groups["Namespace"].Value,
        Classes   = mt.Groups["Class"].Captures
                                      .OfType<Capture>()
                                      .Select (cp => cp.Value)
     })
     .Dump();

Result of Dump() which ultimately you will traverse to populate the tree.:

enter image description here

I leave it to you to properly populate the tree with the data.


Note I would be remiss to remind you that using regex for lexical parsing has its inherent flaws and ultimately it would be better to use a true parsing tool suited for the job.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Hmm you have understood me. I think i need to learn some more. Can you refer me to a link? – Slavi Mar 04 '15 at 10:30
  • And If you know a good parsing tool with some lessons that could help. Thanks. – Slavi Mar 04 '15 at 10:38
  • Error in .Dump(); *(System.Collection.Generic.IEnum.. cannot found .dumb) – Slavi Mar 04 '15 at 10:55
  • @AhmedAlaa `Dump` won't be used in an actual program. It is only available on [Linqpad](http://www.linqpad.net/) to show the current state of an object. Remove dump and handle the dynamic entity that is returned with the data. – ΩmegaMan Mar 04 '15 at 14:43
  • @AhmedAlaa See [Example Sample of lexical Analysis](http://stackoverflow.com/questions/5598288/example-sample-of-lexical-analyzer-in-c-sharp) to get you started. – ΩmegaMan Mar 04 '15 at 15:09