How to extract substring in parentheses using Regex pattern How to extract substring in parentheses using Regex pattern vba vba

How to extract substring in parentheses using Regex pattern


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.RegExpDim 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 IfEnd Function


Not strictly an answer to your question, but sometimes, for things this simple, good ol' string functions are less confusing and more concise than Regex.

Function BetweenParentheses(s As String) As String    BetweenParentheses = Mid(s, InStr(s, "(") + 1, _        InStr(s, ")") - InStr(s, "(") - 1)End Function

Usage:

Debug.Print BetweenParentheses("""Wouldn't It Be Nice"" (B. Wilson/Asher/Love)")'B. Wilson/Asher/Love

EDIT @alan points our that this will falsely match the contents of parentheses in the song title. This is easily circumvented with a little modification:

Function BetweenParentheses(s As String) As String    Dim iEndQuote As Long    Dim iLeftParenthesis As Long    Dim iRightParenthesis As Long    iEndQuote = InStrRev(s, """")    iLeftParenthesis = InStr(iEndQuote, s, "(")    iRightParenthesis = InStr(iEndQuote, s, ")")    If iLeftParenthesis <> 0 And iRightParenthesis <> 0 Then        BetweenParentheses = Mid(s, iLeftParenthesis + 1, _            iRightParenthesis - iLeftParenthesis - 1)    End IfEnd Function

Usage:

Debug.Print BetweenParentheses("""Wouldn't It Be Nice"" (B. Wilson/Asher/Love)")'B. Wilson/Asher/LoveDebug.Print BetweenParentheses("""Don't talk (yell)""")' returns empty string

Of course this is less concise than before!


This a nice regex

".*\(([^)]*)

In VBA/VBScript:

Dim myRegExp, ResultString, myMatches, myMatch As MatchDim myRegExp As RegExpSet myRegExp = New RegExpmyRegExp.Pattern = """.*\(([^)]*)"Set myMatches = myRegExp.Execute(SubjectString)If myMatches.Count >= 1 Then    Set myMatch = myMatches(0)    If myMatch.SubMatches.Count >= 3 Then        ResultString = myMatch.SubMatches(3-1)    Else        ResultString = ""    End IfElse    ResultString = ""End If

This matches

Put Your Head on My Shoulder

in

"Don't Talk (Put Your Head on My Shoulder)"  

Update 1

I let the regex loose on your doc file and it matches as requested. Quite sure the regex is fine. I'm not fluent in VBA/VBScript but my guess is that's where it goes wrong

If you want to discuss the regex some further that's fine with me. I'm not eager to start digging into this VBscript API which looks arcane.

Given the new input the regex is tweaked to

".*".*\(([^)]*)

So that it doesn't falsely match (Put Your Head on My Shoulder) which appears inside the quotes.

enter image description here