Search multiple substrings with the RegExp object

The RegExp object in the Microsoft VBScript Regular Expression type library supports regular expression patterns containing the | (or) operator, which lets you search for multiple substrings at the same time. For example, the following piece of code lets you search for a month name in a source text:

' NOTE: this code requires a reference to the'       Microsoft VBScript Regular Expression type libraryDim re As New RegExpDim ma As Matchre.Pattern = "january|february|march|april|may|june|july|september|october|novem" _    & "ber|december"' case isn't significantre.IgnoreCase = True' we want all occurrencesre.Global = True' we assume that the string to be parsed is in the sourceText variableFor Each ma In re.Execute(sourceText)    Print "Found '" & ma.Value & "' at index " & ma.FirstIndexNext

The code above doesn’t search for whole words, though, and would find false matches such as “marches”. To force the Execute method to search only for whole words, we must embed the list of words among parenthesis, and add the  sequence to specify that the occurrence should be on a word boundary:

re.Pattern = "(january|february|march|april|may|june|july|september|october|no" _    & "vember|december)"

Thanks to the Join function, it is easy to create a generic function that searches for any word in an array:

' Search all the words specified in the array passed as a second argument' returns a bi-dimensional array of variants, where arr(0,n) is the N-th' matched word, and arr(1,n) is the index where the word has been found' NOTE: requires a reference to the'       Microsoft VBScript Regular Expression type libraryFunction InstrAllWords(ByVal Text As String, words() As String, _    Optional IgnoreCase As Boolean) As Variant    Dim re As New RegExp    Dim ma As Match    Dim maCol As MatchCollection    Dim index As Long        ' create the pattern in the form "(word1|word2|....|wordN)"    re.pattern = "(" & Join(words, "|") & ")"    ' we want all occurrences    re.Global = True    ' case insensitive?    re.IgnoreCase = IgnoreCase        ' get the result    Set maCol = re.Execute(Text)        ' now we can DIMension the result array    ReDim res(1, maCol.Count) As Variant        ' move results into the array    For Each ma In maCol        index = index + 1        res(0, index) = ma.Value        res(1, index) = ma.FirstIndex    Next        ' return to caller    InstrAllWords = resEnd Function

Here’s is an example of how you can use the function above:

' fill an array with desired wordsDim words(2) As Stringwords(0) = "Visual": words(1) = "Basic": words(2) = "Windows"Dim arr() as Variantarr = InstrAllWords(txtSource.Text, words())For i = 1 To UBound(arr, 2)    Print "'" & arr(0, i) & "' at index " & arr(1, i)Next

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


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.