查找并替换长度在 2 个符号之间的可变字符串

Find and replace string with variable length between 2 signs

我找遍了,但找不到如何替换两个特殊字符之间的单词的命令。 我想删除两个括号之间的所有内容。

我只删除了一个特殊字符“(”,但没有删除括号中的整个单词。

结果应该是怎样的:

示例 - 之前: 26125 奥尔登堡 (Oldenburg), Alexandersfeld

示例 - 之后: 26125 奥尔登堡, Alexandersfeld

Option Explicit

Sub Bereinigen()

Dim Text As String
'Text = ActiveSheet.Cells(1, 1)
Text = "26125 Oldenburg (Oldenburg), Alexandersfeld"

If InStr(Text, "(") > 0 Then

    Dim strSearchFor As String
    Dim strReplaceWith As String
    Dim strNewText As String

    strSearchFor = "("
    strReplaceWith = ""

    strNewText = Replace(Text, strSearchFor, strReplaceWith)
    Debug.Print strNewText
End If
End Sub

如果您正在编写 VBA 过程,那么您可以结合使用 LeftMidInStr:

Sub sRemoveBracketData()
    Dim strData As String
    strData = "26125 Oldenburg (Oldenburg), Alexandersfeld"
    If (InStr(strData, ")") > 0) And (InStr(strData, ")") > 0) Then
        strData = Left(strData, InStr(strData, "(") - 1) & Mid(strData, InStr(strData, ")") + 1)
        Debug.Print strData
    End If
End Sub

使用工作表函数可以获得类似的结果:

=IFERROR(LEFT(A15,FIND("(",A15)-1) & MID(A15,FIND(")",A15)+1,999),A15)

此致,

UDF cleanText(干净)

代码

Option Explicit

' In a string, removes the string from a first (left) and a second (right)
' specified string (the two specified strings inclusive).
Function cleanText(Text As String, LeftString As String, RightString As String, _
    Optional trimLeft As Boolean = False, _
    Optional trimRight As Boolean) As String

    cleanText = Text

    Dim foundLeft As Long, foundRight As Long
    Dim LeftStr As String, RightStr As String

    foundLeft = InStr(1, Text, LeftString)
    If foundLeft > 0 And foundLeft <= Len(Text) - Len(LeftString) Then
        foundRight = InStr(foundLeft + Len(LeftString), Text, RightString)
        If foundRight > 0 Then
            LeftStr = Left(Text, foundLeft - 1)
            RightStr = Right(Text, Len(Text) - foundRight - Len(RightString) + 1)
            If trimLeft Then LeftStr = Trim(LeftStr)
            If trimRight Then RightStr = Trim(RightStr)
            cleanText = LeftStr & RightStr
        End If
    End If

End Function

Sub cleanTextExample()

    Const Text = "26125 Oldenburg ($(Oldenburg)$) , Alexandersfeld"
    Dim Result As String

    Result = Text: Debug.Print Result
    Result = cleanText(Text, "($(", ")$)"): Debug.Print Result
    Result = cleanText(Text, "($(", ")$)", True): Debug.Print Result
    Result = cleanText(Text, "($(", ")$)", True, True): Debug.Print Result

' Results:
' 26125 Oldenburg ($(Oldenburg)$) , Alexandersfeld
' 26125 Oldenburg  , Alexandersfeld
' 26125 Oldenburg , Alexandersfeld
' 26125 Oldenburg, Alexandersfeld

End Sub

Sub cleanTextExampleSimple() ' Your Case.

    Const Text = "26125 Oldenburg (Oldenburg), Alexandersfeld"
    Dim Result As String

    Result = Text: Debug.Print Result
    Result = cleanText(Text, "(", ")", True): Debug.Print Result

' Results:
' 26125 Oldenburg (Oldenburg), Alexandersfeld
' 26125 Oldenburg, Alexandersfeld

End Sub

您也可以使用正则表达式来做到这一点。

Option Explicit

Sub BereinigenRgEx()
Dim Text As String, outText As String
Dim RgEx As Object
'Text = ActiveSheet.Cells(1, 1)
Text = "26125 Oldenburg (Oldenburg), Alexandersfeld"
Set RgEx = CreateObject("VBScript.RegExp")
With RgEx
    .Global = True
    .Pattern = "\(.+\)"
    outText = .Replace(Text, "")
    Debug.Print outText
End With
End Sub