0

Hello I am currently working on a project to bring in Regular Expressions into Excel using the VBSCRIPT_RegExp_1. I am using the vba code below.

Function RegExGet(aString As String, myExpression As String) As Variant             
    Dim RegEx As New VBScript_RegExp_10.RegExp
    Dim newArray() As String                                                            

    RegEx.Pattern = myExpression                                                        
    RegEx.IgnoreCase = True                                                             
    RegEx.Global = True                                                                 
    Set Matches = RegEx.Execute(aString)                                                

    x = Matches.Count                                                                   
    ReDim newArray(x - 1) As String                                                                                                                                      
    cnt = 0

       For Each Match In Matches                                                        
         newArray(cnt) = Match.Value                                                            
         cnt = cnt + 1                                                               
       Next                                                                            

    RegExGet = newArray()
End Function

The problem with the code is that if returns multiple results, only the first will show up. It will create a 1 by 1 array where any other results besides the first is hidden. Wh

What I am hoping for is a modification so that the array that is produced, at whatever size it may be, concatenates all the results with a ";" between them. Ie "1; 2; 3;". Any tips on how JOIN or CONCATENATE may be used would be most welcome!

Thanks and have a nice day!

1 Answer 1

1

You really just need to add the Join to the last line.

Function RegExGet(aString As String, myExpression As String) As String
    Dim RegEx As New VBScript_RegExp_10.RegExp
    Dim newArray() As String
    Dim x As Long, cnt As Long
    Dim Matches As Object, Match As Object

    RegEx.Pattern = myExpression
    RegEx.IgnoreCase = True
    RegEx.Global = True
    Set Matches = RegEx.Execute(aString)

    x = Matches.Count
    ReDim newArray(x - 1) As String
    cnt = 0

       For Each Match In Matches
         newArray(cnt) = Match.Value
         cnt = cnt + 1
       Next

    RegExGet = Join(newArray(), ";")
End Function

'example
Sub y()

MsgBox RegExGet("123456789", "\d")

End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.