Extracting Text Between Brackets with Regex

后端 未结 4 1359
情书的邮戳
情书的邮戳 2021-01-19 12:24

In sentences like:

\"[x] Alpha

[33] Beta\"

I extract an array of bracketed data as ([x], [33])

using VBA regex Pattern:

\"(\\         


        
4条回答
  •  旧时难觅i
    2021-01-19 13:22

    With Excel and VBA you can strip the brackets after the regex extraction:

    Sub qwerty()
    
        Dim inpt As String, outpt As String
        Dim MColl As MatchCollection, temp2 As String
        Dim regex As RegExp, L As Long
    
        inpt = "38c6v5hrk[x]537fhvvb"
    
        Set regex = New RegExp
        regex.Pattern = "(\[x\])|(\[\d*\])"
        Set MColl = regex.Execute(inpt)
        temp2 = MColl(0).Value
    
        L = Len(temp2) - 2
        outpt = Mid(temp2, 2, L)
    
        MsgBox inpt & vbCrLf & outpt
    End Sub
    

提交回复
热议问题