The following routine extracts all the words from a source string and returns a collection. Optionally, the result contains only unique words. This code is remarkably simpler than an equivalent "pure" VB solution because it takes advantage of the RegExp object in the Microsoft VBScript Regular Expression type library.
Here is an example of how you can use the routine: |
Click here to copy the following block |
Dim v As Variant Dim count As Long
For Each v In GetWords(txtSource.Text) Select Case LCase$(v) Case "the", "a", "an" count = count + 1 End Select Next MsgBox "Found " & count & " articles." |
Click here to copy the following block |
Function GetWords(ByVal Text As String, Optional DiscardDups As Boolean) As _ Collection Dim re As New RegExp Dim ma As Match re.Pattern = "\b\w+\b" re.Global = True Set GetWords = New Collection On Error Resume Next For Each ma In re.Execute(Text) If DiscardDups Then GetWords.Add ma.Value, ma.Value Else GetWords.Add ma.Value End If Next End Function
Dim v As Variant Dim count As Long
For Each v In GetWords(txtSource.Text) Select Case LCase$(v) Case "the", "a", "an" count = count + 1 End Select Next MsgBox "Found " & count & " articles." |
|