尝试这个:
Function GetTextWithinBrackets(kursName As String, left As String, right As String)
klammerAuf = InStr(kursName, left)
klammerZu = InStr(klammerAuf + 1, kursName, right) ' start searching for the closing bracket after the opening one
If klammerAuf * klammerZu Then ' check if both brackets were found
GetTextWithinBrackets = Mid(kursName, klammerAuf + 1, klammerZu - klammerAuf - 1)
Else
GetTextWithinBrackets = "No " & left & " or " & right & " in the text"
End If
End Function
Sub test()
Debug.Print GetTextWithinBrackets("jkdsf]]]klsjc[=the text within brackets=]asd[[aw]]sd", "[", "]")
Debug.Print GetTextWithinBrackets("872639281(=the text within brackets=)slsx(sladmlsa)", "(", ")")
Debug.Print GetTextWithinBrackets("872639281=the text within brackets=]slsx(sladmlsa)", "[", "]")
End Sub
输出:
=the text within brackets=
=the text within brackets=
No [ or ] in the text