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

问题描述 投票:0回答:1

我已经找遍了所有的地方,但是找不到一个命令可以替换两个特殊字符之间的单词。我想删除两个括号之间的所有内容。

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

结果应该是怎样的。

例子 - Before:26125 Oldenburg (Oldenburg), Alexandersfeld.

例子--事后:26125 Oldenburg, 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
excel vba excel-vba if-statement str-replace
1个回答
2
投票

你也可以使用正则表达式来完成这个任务。

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

3
投票

如果你正在编写一个VBA过程,那么你可以使用以下组合来完成这个任务 Left, MidInStr:

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)

致敬。


1
投票

UDF cleanText (Bereinigen)

守则

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
© www.soinside.com 2019 - 2024. All rights reserved.