How to extract substring in parentheses using Regex pattern

前端 未结 6 550
孤城傲影
孤城傲影 2020-12-09 11:36

This is probably a simple problem, but unfortunately I wasn\'t able to get the results I wanted...

Say, I have the following line:

\"Wouldn\'t It B         


        
6条回答
  •  失恋的感觉
    2020-12-09 11:55

    Edit: After examining your document, the problem is that there are non-breaking spaces before the parentheses, not regular spaces. So this regex should work: ""[ \xA0]*\(([^)]+)\)

    ""       'quote (twice to escape)
    [ \xA0]* 'zero or more non-breaking (\xA0) or a regular spaces
    \(       'left parenthesis
    (        'open capturing group
    [^)]+    'anything not a right parenthesis
    )        'close capturing group
    \)       'right parenthesis
    

    In a function:

    Public Function GetStringInParens(search_str As String)
    Dim regEx As New VBScript_RegExp_55.RegExp
    Dim matches
        GetStringInParens = ""
        regEx.Pattern = """[ \xA0]*\(([^)]+)\)"
        regEx.Global = True
        If regEx.test(search_str) Then
            Set matches = regEx.Execute(search_str)
            GetStringInParens = matches(0).SubMatches(0)
        End If
    End Function
    

提交回复
热议问题