Home > Net >  Delimiters in Excel VBA Regex Patterns, filter something but not others
Delimiters in Excel VBA Regex Patterns, filter something but not others

Time:01-12

I wrote to VBA function that removes number sequences between delimiters from a cell in Excel. The issue I am having is that if the string contains text between the delimiters '(' and '),' then from the first delimiter to the end of the string is deleted. The code I have below works fine on delimited numbers.

I need to change the Regex pattern so that if a letter or word is within the delimiter is found, don't do anything, move on to the next set of delimiters, and if there are numbers, remove the delimiters and everything between them.

Function RemoveTags(ByVal Value As String) As String
    Dim rx As New RegExp
    With rx
        .Global = True
        .Pattern = "\((.*\d)\)"
    End With
    RemoveTags = WorksheetFunction.Trim(rx.Replace(Value, ""))
End Function

This one is a good one.
Inputs: Put a stop to Rugby's foul school leader (5,2,3,4)
OutPut: Put a stop to Rugby's foul school leader

With the pattern: "\((.*\d)\)"
Input: Put a (stop to Rugby's) foul school leader (5,2,3,4)
Output: Put a
The above should be: Put a (stop to Rugby's) foul school leader

I have tried adding "\W" and other things to the pattern and have had no luck. I also have been using the regex expressions emulator at enter image description here

Formula in B1:

=MAP(A1:A5,LAMBDA(z,IFERROR(TRIM(CONCAT(TEXTSPLIT(z,LET(x,TEXTSPLIT(z,{"(",")"},,1),"("&TOROW(IF(-SUBSTITUTE(x,",",),x,),2)&")"),,1))),z)))
  • Related