Home > database >  How to select text between two characters in a RichTextBox
How to select text between two characters in a RichTextBox

Time:10-20

I have a RichTextBox that logs information about my app. Here is an example of what it may log:

<22:52:21:179> Starting Argo Studio
<22:52:22:731> Argo Studio has finished starting
<22:52:30:41> Time to load commands: 00:00:00.00
<22:52:30:48> Created 'App 1'

The text between the < and the > is the time.

I need to change the color of the time to gray.

Previously, I did this:

for (int i = 0; i < RichTextBox.Lines.Length; i  )
{
    int indexStart = RichTextBox.GetFirstCharIndexFromLine(i);
    int indexEnd = RichTextBox.Lines[i].Split(' ')[0].Length;
    RichTextBox.Select(indexStart, indexEnd);
    RichTextBox.SelectionColor = Color.Gray;
}

However, this no longer works for me because now I have logs with multiple lines:

<23:0:4:320> Error-h88tzd: The source and destination are the same.
Source: 'C:\Users\user\Dropbox\PC\Desktop\...'.
Destination: 'C:\Users\user\Dropbox\PC\Desktop\....
More information: https://

CodePudding user response:

you can skip the line if > or < do not exist. try this :

        RichTextBox.AppendText("< 22:52:21:179 > Starting Argo Studio"  
       "\r\n"   "<22:52:22:731 > Argo Studio has finished starting"  
       "\r\n"   "<22:52:30:41 > Time to load commands: 00:00:00.00"  
       "\r\n"   "< 22:52:30:48 > Created 'App 1'"  
       "\r\n"   "23:0:4:320 Error - h88tzd: The source and destination are the same."  
       "\r\n"   @"Source: 'C:\Users\user\Dropbox\PC\Desktop\...'."  
       "\r\n"   @"Destination: 'C:\Users\user\Dropbox\PC\Desktop\...."  
       "\r\n"   "More information: https:/");


        for (int i = 0; i < RichTextBox.Lines.Length; i  )
        {
            int indexStart = RichTextBox.Lines[i].IndexOf("<");
            int indexEnd = RichTextBox.Lines[i].LastIndexOf(">");
            if (indexStart < 0 || indexEnd < 0)
                continue;
            int baseIndex = RichTextBox.Text.IndexOf(RichTextBox.Lines[i]);
            RichTextBox.Find(RichTextBox.Lines[i].Substring(indexStart 1, indexEnd-1));
            RichTextBox.SelectionColor = Color.Gray;
        }

then the result:

enter image description here

hope this helps!

CodePudding user response:

You can use Regex.Matches to find all section of the text that match the time stamp.
Each Match object returns the char Index where the match was found and the Length of the matched string.
This information can be used to perform a selection.

It doesn't matter whether the text is wrapped or not, since the whole text is considered.

For example:

var matches = Regex.Matches(richTextBox.Text, @"<*\d :\d :\d :\d >*", RegexOptions.Multiline);
foreach (Match m in matches) {
    richTextBox.SelectionStart = m.Index;
    richTextBox.SelectionLength = m.Length;
    richTextBox.SelectionColor = Color.Gray;
}

Not exactly clear whether you want to select the time stamp here:

Time to load commands: 00:00:00.00

small change required if you do (e.g., set the pattern to <*\d :\d :\d [:.]\d >*)

If you want only the time stamps in angle brackets, then <\d :\d :\d :\d >

CodePudding user response:

Only if the line contains < and >, then format color

for (int i = 0; i < RichTextBox.Lines.Length; i  )
{
    if (RichTextBox.Lines[i].Contains("<") && 
        RichTextBox.Lines[i].Contains(">"))
    {
        int indexStart = RichTextBox.GetFirstCharIndexFromLine(i);
        int indexEnd = RichTextBox.Lines[i].Split(' ')[0].Length;
        RichTextBox.Select(indexStart, indexEnd);
        RichTextBox.SelectionColor = Color.Gray;
    }
}
  • Related