Search code examples
regexvbaregex-group

Regular expression pattern in the string and then also remove the substring


I have a list of string which contains few set of characters and spaces in the beginning before encountered first three digits. if the pattern found then I need to collect only first three digits from the string.

Example1:

String: "SEE 222 Newcastle NSW 203"
Expected output: 222

Example2:

String: "SEE TEMP 212 Newcastle NSW 203"
Expected output: 212

Example3:

String: "SEE TEMP212 Newcastle NSW 203"
Expected output: 212

Solution

  • Sub TestExtractFirstThreeDigits()
        Dim testString1 As String
        Dim testString2 As String
        Dim result As String
        
        testString1 = "SEE 222 Newcastle NSW 203"
        testString2 = "SEE TEMP 212 Newcastle NSW 203"
        
        result = ExtractFirstThreeDigits(testString1)
        Debug.Print "Example 1: " & result ' Expected output: 222
        
        result = ExtractFirstThreeDigits(testString2)
        Debug.Print "Example 2: " & result ' Expected output: 212
    End Sub
    
    Function ExtractFirstThreeDigits(inputString As String) As String
        Dim regex As Object
        Dim matches As Object
        Dim firstThreeDigits As String
        
        ' Create the RegExp object
        Set regex = CreateObject("VBScript.RegExp")
        With regex
            .Pattern = "\b\d{3}\b" ' Pattern to match exactly three digits
            .Global = False ' We only need the first match
        End With
        
        ' Execute the regex search
        Set matches = regex.Execute(inputString)
        
        ' Check if any match is found
        If matches.Count > 0 Then
            firstThreeDigits = matches(0).Value
        Else
            firstThreeDigits = ""
        End If
        
        ' Return the first three digits found
        ExtractFirstThreeDigits = firstThreeDigits
    End Function