Extract words with the RegExp object

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.

' Get a collection of all the words in a string' If the second argument is True, only unique words are returned''' NOTE: requires a reference to the'       Microsoft VBScript Regular Expression type libraryFunction GetWords(ByVal Text As String, Optional DiscardDups As Boolean) As _    Collection    Dim re As New RegExp    Dim ma As Match        ' the following pattern means that we're looking for a word character (w)    ' repeated one or more times (the + suffix), and that occurs on a word    ' boundary (leading and trailing  sequences)    re.Pattern = "w+"    ' search for *all* occurrences    re.Global = True        ' initialize the result    Set GetWords = New Collection        ' we need to ignore errors, if duplicates are to be discarded    On Error Resume Next        ' the Execute method does the search and returns a MatchCollection object    For Each ma In re.Execute(Text)        If DiscardDups Then            ' if duplicates are to be discarded, we just add a key to the             ' collection item            ' and the Add method will do the rest            GetWords.Add ma.Value, ma.Value        Else            ' otherwise just add to the result            GetWords.Add ma.Value        End If    Next    End Function

Here is an example of how you can use the routine:

' Count how many articles appear in a source string' held in the txtSource textbox controlDim v As VariantDim count As LongFor Each v In GetWords(txtSource.Text)    Select Case LCase$(v)        Case "the", "a", "an"            count = count + 1    End SelectNextMsgBox "Found " & count & " articles."

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS