擷取閉合符號中的字串

來源:互聯網
上載者:User
字串 在解決Iif問題時,為了判斷iif語句是否合法,同時找出其中的條件,傳回值True及傳回值False。卻不自然中寫出了一個通用的函數模組,該函數能解決閉合符號形式中的一串文字。如”( … )”、”[ … ]”等,看官也可以自己設定一個閉合符號或字串,但必須是對稱出現,如“AVB”和“Def”便可以組合成一對,這樣對於字串AVBCdeOkDef的閉合符號中。現將這些函數整理如下,參數、傳回值及函數功用都已經在注釋中說明,希望本函數模組能對VB忠實愛好者有所協助。





' --------------------------------------------------------------------------

' 從某一段文字中尋找某一個符號(須考慮大小寫),並且返回該符號的所有位置索引

' douhapy 2005-01-31

'

' 參數:

' strSentence :任意一段文字

' strSymbol :需要尋找的特殊符號,或字串

' SymbolIndex() :返回該符號在文字中的所處位置索引

' blCaseMatch :是否必須大小寫匹配 (True 大小寫必須匹配)

' blDesc :是否降序排列SymbolIndex中的資料(True 為降序排列索引)

'

' 傳回值:

' True 成功找到該符號,同時SymbolIndex有相應的值

' --------------------------------------------------------------------------

Function CheckSymbolFromSentence(ByVal strSentence As String, ByVal strSymbol As String, _

ByRef symbolIndex() As Integer, Optional ByVal blCaseMatch = True, Optional ByVal blDesc = False) As Boolean



Dim intSymbolIndex() As Integer

Dim strTmp As String

Dim intTmp As Integer

Dim blReturn As Boolean



Dim i As Integer



strTmp = strSentence: blReturn = False: i = 0



If blDesc Then

If blCaseMatch Then

intTmp = InStrRev(strTmp, strSymbol)

Else

intTmp = InStrRev(strTmp, strSymbol, -1, vbTextCompare)

End If

Else

If blCaseMatch Then

intTmp = InStr(strTmp, strSymbol)

Else

intTmp = InStr(1, strTmp, strSymbol, vbTextCompare)

End If

End If



Do While intTmp <> 0

blReturn = True

ReDim Preserve intSymbolIndex(i)

intSymbolIndex(i) = intTmp

intTmp = intTmp - 1



If intTmp <> 0 Then

If blDesc Then

If blCaseMatch Then

intTmp = InStrRev(strTmp, strSymbol, intTmp)

Else

intTmp = InStrRev(strTmp, strSymbol, intTmp, vbTextCompare)

End If

Else

If blCaseMatch Then

intTmp = InStr(intTmp + 1, strTmp, strSymbol)

Else

intTmp = InStr(intTmp + 1, strTmp, strSymbol, vbTextCompare)

End If

End If

End If

i = i + 1

Loop



CheckSymbolFromSentence = blReturn

symbolIndex = intSymbolIndex



Erase intSymbolIndex

End Function



' --------------------------------------------------------------------------

' 擷取任意一段文字"( ... )"閉合符號中的字串資料

' douhapy 2005-01-31

'

' 參數:

' strSentence :任意一段文字

' LeftBracketIndex:該段文字中閉合符號左符號的索引

' LeftCloseSymbol :閉合符號的左符號

' RightCloseSymbol:閉合符號的右符號

' blCaseMatch :是否必須大小寫匹配 (True 大小寫必須匹配)

'

' 傳回值

' 若成功 則返回閉合括弧中的字串

' 否則 返回Null 字元串

' --------------------------------------------------------------------------

Function GetCloseString(ByVal strSentence As String, ByVal LeftBracketIndex As Integer, _

Optional ByVal LeftCloseSymbol As String = "(", Optional ByVal RightCloseSymbol As String = ")", _

Optional ByVal blCaseMatch As Boolean = True) As String



Dim strReturn As String

Dim strTmp As String



Dim intLeftBracketIndex() As Integer ' 所有左括弧的位置

Dim intRightBracketIndex() As Integer ' 所有右括弧的位置



Dim i As Integer

Dim j As Integer

Dim m As Integer

Dim mintLeftBracketIndex As Integer

Dim mintRightBracketIndex As Integer



strTmp = strSentence: strReturn = ""

' 尋找第一個左括弧

If blCaseMatch Then

mintLeftBracketIndex = InStr(1, strSentence, LeftCloseSymbol)

Else

mintLeftBracketIndex = InStr(1, strSentence, LeftCloseSymbol, vbTextCompare)

End If



If mintLeftBracketIndex <> 0 Then

If UCase(Mid(strSentence, LeftBracketIndex, Len(LeftCloseSymbol))) = UCase(LeftCloseSymbol) Then

mintLeftBracketIndex = LeftBracketIndex

End If

Else

GoTo EndLab

End If



' 擷取所有的左括弧和右括弧的位置

Call CheckSymbolFromSentence(strTmp, LeftCloseSymbol, intLeftBracketIndex, blCaseMatch, True)

Call CheckSymbolFromSentence(strTmp, RightCloseSymbol, intRightBracketIndex, blCaseMatch, True)



If UBound(intLeftBracketIndex) = UBound(intRightBracketIndex) Then

' 迴圈尋找匹配的左右對稱括弧,同時將資料置為0

For i = 0 To UBound(intLeftBracketIndex)



For j = 0 To UBound(intRightBracketIndex)

If intRightBracketIndex(j) <> 0 Then

If intRightBracketIndex(j) < intLeftBracketIndex(i) Then

Exit For

End If



If j = UBound(intRightBracketIndex) Then

j = j + 1: Exit For

End If



End If

Next



For m = j - 1 To 0 Step -1

If intRightBracketIndex(m) <> 0 Then

If intLeftBracketIndex(i) = mintLeftBracketIndex Then

mintRightBracketIndex = intRightBracketIndex(m)

End If

intRightBracketIndex(m) = 0

Exit For

End If

Next

Next



strReturn = Mid(strTmp, mintLeftBracketIndex + Len(LeftCloseSymbol), _

mintRightBracketIndex - mintLeftBracketIndex - Len(RightCloseSymbol))

End If



EndLab:

GetCloseString = strReturn

Erase intLeftBracketIndex

Erase intRightBracketIndex

End Function





' ----------------------------------------------------------------------------------------------------------------------------------------------------

' 檢查IIF語句中放在對應"( )"內的語句中的條件運算式、True運算式、False運算式

' douhapy 2005-01-31

'

' 參數:

' strSentence :任意一條語句(該語句含有IIf)

' strCondition :返回該IIf語句中的條件運算式

' strReturnT :返回該IIf語句中的True運算式

' strReturnF :返回該IIf語句中的False運算式

'

' 傳回值:

' True 成功尋找到所需的運算式

' ----------------------------------------------------------------------------------------------------------------------------------------------------

Function CheckIIFSentence(ByVal strSentence As String, Optional ByRef strCondition As String = "", _

Optional ByRef strReturnT As String = "", Optional ByRef strReturnF As String = "") As Boolean



Dim strTmp As String



Dim strIIfSentence As String

Dim mstrCondition As String ' IIf語句中的條件

Dim mstrReturnT As String ' IIf語句中的True結果

Dim mstrReturnF As String ' IIf語句中的False結果



Dim intTmp1 As Integer

Dim intTmp2 As Integer



Dim i As Integer

Dim j As Integer

Dim m As Integer



Dim blSucceed As Boolean

Dim intLeftBracketIndex() As Integer ' 所有左括弧的位置

Dim intRightBracketIndex() As Integer ' 所有右括弧的位置



' --------------------------------------------------------------------------

' 先尋找iif (,判斷是否為iif語句

strTmp = Replace(strSentence, " ", "")

If InStr(1, strTmp, "iif(", vbTextCompare) = 0 Then

Exit Function

End If



' --------------------------------------------------------------------------

' 擷取IIf中的運算式

'

strTmp = strSentence

intTmp1 = InStr(1, strTmp, "iif", vbTextCompare)

If intTmp1 Then



' 擷取離IIf最近的左括弧的位置,並儲存

intTmp1 = InStr(intTmp1, strTmp, "(")



strIIfSentence = GetCloseString(strTmp, intTmp1)

blSucceed = True

End If



' --------------------------------------------------------------------------

' 擷取IIf中的條件以及傳回值

If blSucceed Then

blSucceed = False



' 擷取條件

intTmp1 = InStr(1, strIIfSentence, ",", vbTextCompare)

If intTmp1 <> 0 Then

mstrCondition = Mid(strIIfSentence, 1, intTmp1 - 1)



intTmp2 = InStr(intTmp1 + 1, strIIfSentence, ",", vbTextCompare)

If intTmp2 <> 0 Then



' 擷取傳回值

mstrReturnT = Mid(strIIfSentence, intTmp1 + 1, intTmp2 - intTmp1 - 1)

mstrReturnF = Mid(strIIfSentence, intTmp2 + 1, Len(strIIfSentence) - intTmp2)



blSucceed = True

End If

End If

End If



CheckIIFSentence = blSucceed

strCondition = mstrCondition

strReturnT = mstrReturnT

strReturnF = mstrReturnF



End Function



Private Sub Command1_Click()

Dim strTmp As String

Dim strCondition As String ' IIf語句中的條件

Dim strReturnT As String ' IIf語句中的True結果

Dim strReturnF As String ' IIf語句中的False結果



strTmp = "IIf (((A+B)-(B+A))- ((B-6)-C) * A ,StandOut,OutTime ) - (StandOut -OutTime /2) > (Standout + OutTime)"

If CheckIIFSentence(strTmp, strCondition, strReturnT, strReturnF) Then

MsgBox "原語句:" & vbCrLf & strTmp & vbCrLf & _

"IIf語句中的條件: " & strCondition & vbCrLf & vbCrLf & _

"IIf語句中的True傳回值: " & strReturnT & vbCrLf & _

"IIf語句中的False傳回值: " & strReturnF



MsgBox GetCloseString(strTmp, 57)



End If



strTmp = "{[[A123BEFGCB[[[["

MsgBox GetCloseString(strTmp, 4, "{[[A", "[[[[", False)



End Sub



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。