【发布时间】:2019-05-09 20:53:02
【问题描述】:
我的程序必须在 RichTextBox 中找到特定的单词并更改它们的颜色(简单的语法高亮显示)。我正在使用Regex 来查找单词。
我能找到它们,但如果我的文本包含 2 个或更多相同的单词,我只能更改第一个的颜色,其他的保持不变。
Dim words As String = "(in|handles|object|sub|private|dim|as|then|if|regex)"
Dim rex As New Regex(words)
Dim mc As MatchCollection = rex.Matches(RichTextBox1.Text.ToLower)
Dim lower_case_text As String = RichTextBox1.Text.ToLower
For Each m As Match In mc
For Each c As Capture In m.Captures
MsgBox(c.Value)
Dim index As Integer = lower_case_text.IndexOf(c.Value)
Dim lenght As Integer = c.Value.Length
RichTextBox1.Select(index, lenght)
RichTextBox1.SelectionColor = Color.Blue
Next
Next
我的代码需要通过单击按钮运行。我认为我的问题出在for each 循环中,但我不确定。
我已经有几个版本了,但都没有。
【问题讨论】:
标签: regex vb.net winforms richtextbox