我有一个RichTextBox,它可以记录有关我的应用程序的信息。下面是它可能记录的内容的示例:
<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'
<
和>
之间的文本是时间。
"我需要将时间的颜色改为灰色“
以前,我这样做:
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;
}
但是,这对我不再有效,因为现在我有多行日志:
<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://
3条答案
按热度按时间qnzebej01#
如果
> or <
不存在,您可以跳过这一行。请尝试以下操作:则结果为:
希望这对你有帮助!
syqv5f0l2#
仅当行包含
<
和>
时,才格式化颜色toiithl63#
您可以使用Regex.Matches来查找与时间戳匹配的文本的所有部分。
每个Match对象会传回找到相符项目的字符
Index
,以及相符字串的Length
。该信息可用于执行选择。
文本是否换行无关紧要,因为考虑的是整个文本。
例如:
不清楚是否要在此处选择时间戳:
如果这样做,则需要进行少量更改(例如,将模式设置为
<*\d+:\d+:\d+[:.]\d+>*
)如果只需要尖括号中的时间戳,则
<\d+:\d+:\d+:\d+>