Multiple match in RE

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi

To get multiple matches you have to set the .Global property to True (the default is false).

This is an example.
I changed the pattern because I don't see why to use submatches on such a simple problem.

I added another parameter to the function that allows you to say which bracket pair you want.

Try:

Code:
Function MyGetParen(strIn As String, lInd As Long) As String
    Dim objRegex As Object
    Dim objRegMC As Object
    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Pattern = "\([^)]+\)"
        .Global = True
        Set objRegMC = .Execute(strIn)
        If objRegMC.Count >= lInd Then
            MyGetParen = objRegMC(lInd - 1)
        Else
            MyGetParen = "No match"
        End If
    End With
    Set objRegex = Nothing
End Function

Sub Test()
MsgBox GetParen("Not me")
MsgBox MyGetParen("V2397(+60)(-50)(100)", 1)
MsgBox MyGetParen("V2397(+60)(-50)(100)", 2)
MsgBox MyGetParen("V2397(+60)(-50)(100)", 3)
MsgBox MyGetParen("V2397(+60)(-50)(100)", 4)
End Sub
 
Last edited:
Upvote 0
Hi PGC.. that sorts that, he hadn't included it. In my case I don't know in advance which brackets, so want to know if there's more than one
and can see from your code that's objRegMC.Count.

Cool, thanks :biggrin:
 
Upvote 0
I'm glad it helped.

P. S. I assumed that this is to study regex syntax.
You could use a much simpler function for the same purpose, like:

Code:
Function MyGetParen(strIn As String, lInd As Long) As String
MyGetParen = "No match"
On Error Resume Next
MyGetParen = Split(Split(strIn, "(")(lInd), ")")(0)
End Function

I tested with:

Code:
Sub Test()
MsgBox MyGetParen("Not me", 1)
MsgBox MyGetParen("V2397(+60)(-50)(100)", 1)
MsgBox MyGetParen("V2397(+60)(-50)(100)", 2)
MsgBox MyGetParen("V2397(+60)(-50)(100)", 3)
MsgBox MyGetParen("V2397(+60)(-50)(100)", 4)
End Sub
 
Last edited:
Upvote 0
Yes, quite neat ! Just wondering how to add a bracketed items count to it...
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top