繁体   English   中英

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

[英]Index out of bounds of array error when parsing font string?

我不断在第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

尝试

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM