views:

291

answers:

2

hello all,

i keep getting an "Index is out of bounds of array" error on line # 574 which is:

label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));

... The following text file i am parsing contains this exact information:

Label
"hi tyler"
23, 76
Arial,12.5

...I can successfully parse all the other info (just not the very last line), and the code i have is:

MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)");
foreach (Match match in lines)
{
    string control = match.Groups[1].Value;
    string text = match.Groups[2].Value;
    int x = Int32.Parse(match.Groups[3].Value);
    int y = Int32.Parse(match.Groups[4].Value);
    String cfont = match.Groups[5].Value;
    string color = match.Groups[6].Value;

    Console.WriteLine("{0}, \"{1}\", {2}, {3}, {4}, {5}", control, text, x, y, cfont, color);



    switch (control)
    {
        case "Label":
            Label label = new Label();    

            label.Text = text;

            label.AutoSize = true;
            label.IsAccessible = true;

            label.MouseClick += new MouseEventHandler(label_MouseClick);
            label.MouseDoubleClick += new MouseEventHandler(label_MouseDoubleClick);
            label.MouseDown += new MouseEventHandler(label_MouseDown);
            label.MouseMove += new MouseEventHandler(label_MouseMove);
            label.MouseUp += new MouseEventHandler(label_MouseUp);

            label.Location = new Point(x, y);
            canvas.Controls.Add(label);

                            String fontName = cfont;
    String[] fontNameFields = fontName.Split(',');


    label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));

...i think there may be something wrong with the regex that gets the font stuff... i dont know, but it just won't work, can somebody please help?

For a history of this problem, see: http://stackoverflow.com/questions/1519477/parsing-font-info-and-converting-it-to-system-drawing-font/1519491#1519491

A: 

This regex

(.+?)\n.?\"(.+?)\".?(\d+),\s*(\d+)\n(.+?),(\d+.?\d+)\n?

will get you what you want.
This regex will also handle multiple descriptions like the sample you gave within one file.

The "out-of-bounds" error is caused by the comma being parsed out of the 'cfont' by the regex. Like a commenter suggested, use messagebox or similar to print out what your variables actually contain - that will be just standard debug methods.

slashmais
thank u for the answer, but i get 3errors (red squigly lines) under your regex:MatchCollection lines = Regex.Matches(File.ReadAllText(Path), @"(.+?)\n"([^""]+)"\n(\d+),\s*(\d+)\n(.+?),(.*)";first one says "Method name expected" and second says "Invalid expression term '['" and third says "only assignment, call, increment, decrement and new object expressions can be used as a statement"
baeltazor
thank you for the fix, but not quit yet, but better... it says "Unrecognized escap sequence under the d+ and the s and the last d+
baeltazor
sorry slashmais... same errors but under the question marks now
baeltazor
use mjv's regex - you found that it works.
slashmais
but it doesn't solve my original problem, which is the out of bounds error, so no matter which correct regex i use, it won't matter since i've just figured out that the regex isn't causing the problem
baeltazor
A: 

Try

@"(.+?)\r\n""([^""]+)""\r\n(\d+), (\d+)\r\n(\w+,\d+\.?\d)"

It is similar to slashmais solution, only a bit more restrictive in terms of what is a font and its size, and closer to your original, which I like because the blocks are more explicitly visible.

mjv
thank you mjv - there r no errors on your regex which i reallt like, however, i am still getting than silly out of bounds error on line:label.Font = new Font(fontNameFields[0], Single.Parse(fontNameFields[1]));
baeltazor
@baeltazor I failed to see that your logic expected this font line as one single token, later to be split, before the Font = new Font() line in the program. Check in a minute, I'll edit the regex accordingly!
mjv