Home > Blockchain >  Bug in regex matching or in richTextBox.Select?
Bug in regex matching or in richTextBox.Select?

Time:12-28

Info:

I made a function to colorize richtextbox texts, i open php configuration files in the richtextbox.

Bug:

Everything works fine and the text is colored like i want when i open the file. But after i save the RichTextBox text back to the file, the file type changes from "Unix LF" to "Win CRLF", and the colors i set go wrong. Colors are wrong even after restarting the application. If i change config.php manually back to "Unix LF", the colors are ok again. Also creating any text file as "Unix LF" in notepad and manually changing it to "Win CRLF" and then opening the file in my application causes the same bug.

So it looks like "regExp.Matches" or "richTextBox.Select(match.Index, match.Length)" cant match the file properly for some reason?

Any ideas? or different approaches to coloring block comments? (the other comment types work fine on my code, only block comments have this bug)

I also tried to convert the line endings to CRLF before processing with regex, but it did not affect the bug.

[bug][1] [1]: https://i.stack.imgur.com/92SNu.png

code:

// READ FILE
    public Form_Config()
    {
        // read config.php
        ConfigTextBox.Text = File.ReadAllText("config.php", Encoding.UTF8); // uses RichTextBox

        // tested this also to fix the line endings, but no effects on the bug
        //ConfigTextBox.Text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n"); // convert to CRLF

        // colorize text box
        RichTextBox_Colorize(ConfigTextBox);
    }


// SAVE FILE
    private void Button_Config_Save_Click(object sender, EventArgs e)
    {
        ConfigTextBox.SaveFile("config.php", RichTextBoxStreamType.PlainText);
    }


// RichTextBox_Colorize
    private void RichTextBox_Colorize(RichTextBox richTextBox)
    {
        // background color
        richTextBox.BackColor = Color.Black;

        // text color
        richTextBox.ForeColor = Color.Yellow;

        // comment color
        var CommentColor = Color.Lime;


// BUG start
        // colorize "/*...*/" block comments (bugs with unix->win files, line endings causing it?)
        Regex regExp = new Regex(@"/\*.*?\*/", RegexOptions.IgnoreCase | RegexOptions.Singleline);
        foreach (Match match in regExp.Matches(richTextBox.Text))
        {
            richTextBox.Select(match.Index, match.Length);
            richTextBox.SelectionColor = CommentColor;
        }
// BUG end

        // colorize "#" comments (works fine, no bugs)
        Regex regExp2 = new Regex(@"#(.*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        foreach (Match match in regExp2.Matches(richTextBox.Text))
        {
            richTextBox.Select(match.Index, match.Length);
            richTextBox.SelectionColor = CommentColor;
        }

        // colorize "//" comments (works fine, no bugs)
        Regex regExp3 = new Regex(@"//(.*)", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        foreach (Match match in regExp3.Matches(richTextBox.Text))
        {
            richTextBox.Select(match.Index, match.Length);
            richTextBox.SelectionColor = CommentColor;
        }

        // scroll caret back to start
        richTextBox.Select(0,0);
        richTextBox.ScrollToCaret();
    }

CodePudding user response:

SOLVED

okay, so the problem is that RichTextBox uses \n as newline... so here is the fix (run before regex):

 // convert to LF (RichTextBox can use only \n as newline)
string foobar = ConfigTextBox.Text.Replace("\r\n", "\n").Replace("\r", "\n");
ConfigTextBox.Text = foobar;

  • Related