Extract words with the RegExp object

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as