Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Parser in C# using regex  (Read 3999 times)

0 Members and 1 Guest are viewing this topic.

MyName

  • Guest
Parser in C# using regex
« on: October 09, 2006, 04:20:05 PM »
I would like to do a parser in c# that return the command et the params in a string[].  The initial command comes from a textbox it goes like this :

You type something like this :
*****
Add(param1,param2)
*****
and it is supposed to return something like this :
*****
"Add","param1","param2"
*****
so i can use it after, but i have a

System.IndexOutOfRangeException

What's wrong with that code???

Code: [Select]
 private void Parse()
  {
   string[] Commande=ObtenirCode(this.Tb1.Lines[this.Tb1.Lines.Length-1]);
   //Using string[] here to do the command "effect"
  }
  private string[] ObtenirCode(string LigneCode)
  {
   System.Text.RegularExpressions.Regex Parse=new Regex(@"^((?:[a-z0-9])*)\s*(?:\(|\s+)\s*((?:[a-z0-9])*)\s*(?:(?:,|\s+)\s*([a-z0-9]*)\s*)*\s*(?:\)|\);)?$",RegexOptions.IgnoreCase);
   Match Line=Parse.Match(LigneCode);
   if(!(Line.Groups.Count>255) && !(Line.Groups.Count<1))
   {
    byte Dmn=2;
    if(Line.Groups.Count==3)
    {
     Dmn+=(byte)Line.Groups[3].Captures.Count;
    }
    string[] Resultat=new String[Dmn];
    Resultat[0]=Line.Groups[1].ToString();
    Resultat[1]=Line.Groups[2].ToString();
    CaptureCollection CC=Line.Groups[3].Captures;
    for(byte j=0;j<CC.Count;j++)
    {
     Resultat[j+2]=CC[j].ToString(); //It says the mistake is around here//
    }
    return Resultat;
   }
   else
   {
    this.Tb1.Text="Error!, The number or parameters should be between 1 & 255.";
    string[] Resultat={"Error","Error"};
    return Resultat;
   }
  }

I know the mistake is probably a stupid thing, but i'm anewbie in c# and i wanna know what to do.  Thank you.