Search code examples
regexvb6richtextbox

vb6 select whole word in string


I am trying to search a string without regex perhaps a bad idea !

The search is acting on a string of text in a RichText Box
If I search for "is" the first word in the String is "This"
The last two letters of "This" is are highlighted in vbRed
The string to be searched has two other occurrences of "is" and these are found and highlighted as expected

The question can I prevent the "is" in "This" from being found?

Private Sub btnSearch_Click()

Dim pos As Integer
Dim strToFind As String
Dim Y As Integer
Dim Ask As String

pos = 1
strToFind = tbSearch.Text

Do
    strToFind = tbSearch.Text
    pos = InStr(1, strToSearch, strToFind)
    
    For Y = 1 To Len(strToSearch)
       
    Ask = MsgBox("Yes Next Occurrence or No To Exit ?", vbYesNo, "Question")
    
    If Ask = vbYes Then
    lbOne.AddItem pos
    tbAns.Text = pos

        If pos = 0 Then
            Exit Sub
        End If
        
    rtbOne.SelStart = pos - 1
    rtbOne.SelLength = Len(strToFind)
    rtbOne.SelColor = vbRed
    
    pos = InStr(pos + 1, strToSearch, strToFind)
        
    Else
    
    tbAns.Text = "NO"
    pos = InStr(pos + 1, strToSearch, strToFind)
    tbAns.Text = pos
    Exit Sub
    End If

    Next

    Loop Until pos > 0
End Sub

Private Sub Form_Load()

    strToSearch = "This is a lot of text that will be loaded in the lbText and we will search it is it a case sensative Search"
    
    rtbOne.Text = strToSearch
    tbSearch.Text = "is"

End Sub

If this is not possible a few suggestions on how to use regex
I know this much I need to add the Reference and this might be the
Pattern myRegExp.Pattern = "(.)\strToFind\b(.)"


Solution

  • I was intrigued by the idea of a "built-in" search, as suggested by Bob77 and Mark, so I put together code to implement this idea. The code uses a WinAPI call but is really pretty simple overall and supports moving forward and backward along with toggles for case sensitivity and whole words:

    Option Explicit
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const WM_USER = &H400&
    Private Const EM_GETOLEINTERFACE = (WM_USER + 60)
    
    Private Enum Direction
       Forward = 1
       Backward = -1
    End Enum
    
    Private Doc As ITextDocument
    
    Private Sub Form_Load()
       RichTextBox1.HideSelection = False
       RichTextBox1.Text = "is This is a lot of text that will be loaded in the lbText and we will " & _
                           "search it is it a case sensative Search" & vbCr & vbCr & _
                           "is and IS and is"
       
       SearchTerm.Text = "is"
    
       Dim Unknown As IUnknown
       SendMessage RichTextBox1.hwnd, EM_GETOLEINTERFACE, 0&, Unknown
       Set Doc = Unknown
    End Sub
    
    Private Sub cmdForward_Click()
       Match SearchTerm.Text, chkWhole.Value, chkCase.Value, Forward
    End Sub
    
    Private Sub cmdBack_Click()
       Match SearchTerm.Text, chkWhole.Value, chkCase.Value, Backward
    End Sub
    
    Private Sub Match(ByVal SearchTerm As String, ByVal WholeWords As Integer, ByVal CaseSensitive As Integer, ByVal Direction As Direction)
       Dim Flags As Long
       Flags = 2 * WholeWords + 4 * CaseSensitive
       Doc.Selection.FindText SearchTerm, Direction * Doc.Selection.StoryLength, Flags
    End Sub
    

    You will need to add a reference to RICHED20.dll using the "Browse..." button in Project|References.