Search code examples
excelregexvbaregexp-replace

regex issue in extract M followed by 8 digits


excel value=M#9094562;M 0567468;M25969028;M25969029;Mployee e

Function simpleCellRegex(Myrange As Range) As String

Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim strOutput As String
    
strPattern = "m[0-9]{8}"
    
    
    If strPattern <> "" Then
        strInput = Myrange.Value
        strReplace = ""
        
        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = True
            .Pattern = strPattern
        End With
        
        If regEx.Test(strInput) Then
            simpleCellRegex = regEx.Replace(strInput, strReplace)
        Else
            simpleCellRegex = "Not matched"
        End If
    End If
End Function

Solution

  • Use a regex tester to check what you are getting. https://regexr.com/

    Your current Regex only matches 2 occurrences in your pattern.

    enter image description here

    The others contain a space and a hash, and one only has 7 digits. If you want to find these as well you can expand your pattern.

    m.?[0-9]{7,8}

    enter image description here

    But, if you truly want M followed by 8 digits (M12345678) then your pattern works.

    Your Excel code is wrong... you are not looping through the matches (see below):

    If regEx.Test(strInput) Then
        simpleCellRegex = regEx.Replace(strInput, strReplace)
    Else
    

    You need to loop through the matches in the string (see below):

    Function simpleCellRegex(Myrange As Range) As String
    
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    
    'Added to store matches
    Dim objRegMC As Object
    Dim objRegM As Object
    Dim intCounter As Integer
    Dim strDelimiter As String
    intCounter = 1
    strDelimiter = "|"
    
    strPattern = "m[0-9]{8}"
    
        If strPattern <> "" Then
            strInput = Myrange.Value
            
            With regEx
                .Global = True
                .MultiLine = True
                .IgnoreCase = True
                .Pattern = strPattern
            End With
    
            If regEx.Test(strInput) Then
            
                'Store matched string and loop through matches
                Set objRegMC = regEx.Execute(strInput)
    
                For Each objRegM In objRegMC
                    'Do what you want here to split values etc.
                    'I have added a delimiter, you can do whatever you want
                    
                    If objRegMC.Count > 1 Then
                        If intCounter = objRegMC.Count Then
                            simpleCellRegex = simpleCellRegex & objRegM
                        Else
                            simpleCellRegex = simpleCellRegex & objRegM & strDelimiter
                        End If
                        intCounter = intCounter + 1
                    Else
                        simpleCellRegex = objRegM
                    End If
                Next
          
            Else
                simpleCellRegex = "Not matched"
            End If
            
        End If
    End Function
    

    Results below:

    enter image description here