怎么样找到与(匹配的)或者与)匹配的(
比如:(()、(())、(()())、(()(……

解决方案 »

  1.   

    自己写咯,我刚刚用的,可能和你要的有些出入,你自己参考着改改吧Public Function FindKey(strValue As String, str1 As String, str2 As String)
        Dim i As Long
        Dim j As Long
        i = 1
        i = InStr(1, strValue, str1)
        While i > 0
            j = i
            i = InStr(i + 1, strValue, str2)
            If i > 0 Then
                If FindKey = "" Then
                    FindKey = Mid(strValue, j + 1, i - j - 1)
                Else
                    FindKey = FindKey & "," & Mid(strValue, j + 1, i - j - 1)
                End If
                i = InStr(i + 1, strValue, str1)
            End If
        Wend
    End Function
      

  2.   

    '函数功能:查找字符串sString中,第cCharPos个位置的"("或")"匹配的括号在字符串sString中的位置
    '返回:0:没有相匹配的左括号或者右括号
           其它正整数:相匹配括号的位置Public Function MatchPos(sString As String, cCharPos As Integer) As Integer
        Dim i As Integer, iStep As Integer
        Dim lTemp As Long, lLoop As Long    Select Case Mid(sString, cCharPos, 1)
            Case "(" '向右匹配
                lLoop = Len(sString)
                iStep = 1
                i = -1
            Case ")" '向左匹配
                lLoop = 1
                iStep = -1
                i = 1
            Case Else
                MatchPos = 0
                Exit Function
        End Select
        
        '开始尝试寻找匹配的字符对
        For lTemp = cCharPos + iStep To lLoop Step iStep
            Select Case Mid(sString, lTemp, 1)
                Case "("
                    i = i - 1
                Case ")"
                    i = i + 1
            End Select
            If i = 0 Then
                MatchPos = lTemp
                Exit Function
            End If
        Next lTemp
        MatchPos = 0
    End Function
    '调用方法
    MsgBox "与字符串“1+2*(2+4*(3+4))+110”第5个字符匹配的括号出现在" & MatchPos("1+2*(2+4*(3+4))+110", 5)