我想你有两个问题
- 要匹配的模式。我用了
".*?(\(" & strIn1 & "[^\)]+" & strIn2 & "\))"
- 返回匹配的部分
关键部分是,即在第二个字符串之前的任何地方
"[^\)]+"
都不能有右括号
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