无法弄清楚如何在每次点击按钮时移动到富文本框中的下一个匹配项

时间:2013-07-25 18:12:44

标签: regex vb.net richtextbox

我是新手(请温柔)。我已将我的问题研究成死亡 - 无法找到答案。我很确定我忽略了一些非常明显的东西。我在下面的代码中找到了richtextbox中字符串中的第一个匹配项。我希望能够在每个按钮点击之间移动匹配(下一个匹配/上一个匹配)(下一个匹配的一个按钮/上一个匹配的第二个按钮)。有人可以帮帮我吗?感谢。

Private Sub Button3_Click_5(sender As System.Object, e As System.EventArgs) Handles btnFocus.Click

    Dim MatchStart As Integer = -1
    Dim MatchLength = -1
    Dim MatchResult = Regex.Match(rtb.Text, rgxMyRegex)

    If MatchResult.success Then
        MatchStart = MatchResult.index
        MatchLength = MatchResult.length

        rtb.SelectionStart = MatchStart
        rtb.ScrollToCaret()
        rtb.Focus()

    End If

End Sub

3 个答案:

答案 0 :(得分:1)

使用类似

的内容
findTextAndHighlight(TextBox1.Text, RichTextBox1)

调用

的函数
Sub findTextAndHighlight(ByVal searchtext As String, ByVal rtb As RichTextBox)
    Dim textEnd As Integer = rtb.TextLength
    Dim index As Integer = 0
    Dim fnt As Font = New Font(rtb.Font, FontStyle.Bold)
    Dim lastIndex As Integer = rtb.Text.LastIndexOf(searchtext)
    While (index < lastIndex)
        rtb.Find(searchtext, index, textEnd, RichTextBoxFinds.WholeWord)
        rtb.SelectionFont = fnt
        rtb.SelectionLength = searchtext.Length
        rtb.SelectionColor = Color.Red
        rtb.SelectionBackColor = Color.Cyan
        index = rtb.Text.IndexOf(searchtext, index) + 1
    End While
End Sub

当然,它突出了所有这些,但您肯定可以修改此代码以满足您的需求:P

希望这有帮助

Sources

答案 1 :(得分:0)

您可以在此处获得执行所需操作的代码的更新版本:

Private Sub Button3_Click_5(sender As System.Object, e As System.EventArgs) Handles Button3.Click
    Dim MatchStart As Integer = -1
    Dim MatchLength = -1

    Dim count As Integer = -1
    Dim remainingText As String = rtb.Text
    Dim curText As String = ""
    Dim completed As Boolean = False
    Dim curStartIndex As Integer = 0
    Do
        completed = True
        Dim MatchResult = Regex.Match(remainingText, rgxMyRegex)

        If MatchResult.Success Then
            completed = False
            MatchStart = MatchResult.Index
            MatchLength = MatchResult.Length

            curStartIndex = curStartIndex + MatchLength 'Referred to the rtb index
            curText = remainingText.Substring(MatchStart, MatchLength)
            remainingText = rtb.Text.Substring(curStartIndex, rtb.Text.Length - curStartIndex)

            'If you want to select the last bit being analysed, you have to do:
            rtb.Select(curStartIndex - MatchLength, MatchLength)

        End If
    Loop While (Not completed)

End Sub

如果rgxMyRegex等于“12”且rtb包含“121212”,则此代码将迭代3次,相应的变量将相应地填充(即“12”和“1212”作为剩余文本,“12”和“12”作为剩余文本......)。

答案 2 :(得分:0)

您的应用程序需要在按钮单击之间保持至少足够的状态以突出显示下一个或上一个匹配项。我建议使用“匹配”正则表达式方法预先提取所有匹配项的集合,然后将该集合保留为您使用点击处理程序访问的状态变量。看起来像这样:

Public Class Form1
    Private RegexPattern As String = "sample"
    Private MatchState As MatchCollection  'The current state of all matches
    Private MatchPosition As Integer  'The current index into the MatchState collection

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Put some sample text in the RTB
        Me.RichTextBox1.Text = "Here sample is sample some sample text sample."

        'Load the initial state of the application
        Me.MatchState = Regex.Matches(Me.RichTextBox1.Text, Me.RegexPattern)

        'Highlight the first match
        Me.MatchPosition = 0
        Me.HighlightCurrentPosition()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Move the match backward
        Me.MatchPosition -= 1
        If Me.MatchPosition < 0 Then
            Me.MatchPosition = Me.MatchState.Count - 1
        End If

        Me.HighlightCurrentPosition()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Move the match forward
        Me.MatchPosition += 1
        If Me.MatchPosition >= Me.MatchState.Count Then
            Me.MatchPosition = 0
        End If

        Me.HighlightCurrentPosition()
    End Sub

    Private Sub HighlightCurrentPosition()
        If Me.MatchPosition >= 0 And Me.MatchPosition < Me.MatchState.Count Then
            Dim match = Me.MatchState(Me.MatchPosition)
            Me.RichTextBox1.Focus()
            Me.RichTextBox1.Select(match.Index, match.Length)
        End If
    End Sub
End Class

您必须决定应用生命周期中哪个事件最有效地运行正则表达式来生成状态。在这里,我只是在表单加载时运行它,但是你可以将它调整为适合你的任何东西。