2つの質問があると思います
- 一致するパターン。使った
".*?(\(" & strIn1 & "[^\)]+" & strIn2 & "\))"
- 一致する部分を返す
重要な部分は、つまり、2 番目の文字列の前に右ブラケット
"[^\)]+"
を配置できないことです。
Function ValidateText(strIn1 As String, strIn2 As String, strTarget As String) As String
Dim objRegex As Object
Dim bMatch As Boolean
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
s = ".*?\(.*(" & strIn1 & "[^\)]+" & strIn2 & ".*\))"
.Pattern = ".*?\(.*(" & strIn1 & "[^\)]+" & strIn2 & ").*\)"
bMatch = .TEst(strTarget)
If bMatch Then
ValidateText = "this portion matched " & vbNewLine & .Replace(strTarget, "$1") & vbNewLine & "for " & strIn1 & " " & strIn2
Else
ValidateText = "no match for " & vbNewLine & .Replace(strTarget, "$1") & vbNewLine & "for " & strIn1 & " " & strIn2
End If
End With
End Function
Sub Test1()
MsgBox ValidateText("ccc", "yyy", "(aaa, xxx (ccc,ddd, yyy) abc)")
MsgBox ValidateText("ccc", "yyy", "(aaa, xxx (ccc, abc) yyy)")
End Sub