解析字体字符串时索引超出数组错误?

发布于 2024-08-06 11:26:16 字数 1875 浏览 3 评论 0原文

我在第 574 行不断收到“索引超出数组范围”错误,即:

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

...我正在解析的以下文本文件包含这个确切的信息:

Label
"hi tyler"
23, 76
Arial,12.5

...我可以成功解析所有其他信息(只是不是最后一行),并且我拥有的代码是:

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]));

...我认为可能有获取字体内容的正则表达式有问题...我不知道,但它就是行不通,有人可以帮忙吗?

有关此问题的历史记录,请参阅:解析字体信息并将其转换为System.Drawing.Font

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: Parsing font info and converting it to System.Drawing.Font

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南巷近海 2024-08-13 11:26:16

Try

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

It 与slashmais 解决方案类似,只是在字体及其大小方面有更多限制,并且更接近您的原始解决方案,我喜欢它,因为块更明确可见。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文