请高手们帮忙。
我的字符串如下:s="[中国] (湖南)[浙江],海南、[江苏]    *(黑龙江)"
希望得到result="[中国] [浙江] [江苏]  "
即去除其他,保留全部含中括号的,中间用空格分割。

解决方案 »

  1.   


    '引用Microsoft VBScript Regular Expressions 5.5.Private Sub Command1_Click()
        Dim regexpObj As New RegExp
        regexpObj.Pattern = "\[[\u4e00-\u9fa5]+\]"
        regexpObj.Global = True
        Dim matches, match, ret
        Set matches = regexpObj.Execute("[中国] (湖南)[浙江],海南、[江苏]    *(黑龙江)")    
        For Each match In matches
        
          ret = ret & " " & match.Value
        Next
        MsgBox ret
    End Sub
      

  2.   

    能不能直接使用您这个正则表达式的“取反”表达式,然后用replace方法直接把找到的内容替换为一个空格,如果可以的话,应该怎么改呢?
      

  3.   

    Private Sub Command1_Click()'方法一
    Dim aa As String
    Dim bb As String
    Dim a As Integer
    aa = "[weof] (wfndo) sfon ,[ww] nosdf n[a]"
    For i = 1 To Len(aa)
        If Mid(aa, i, 1) = "[" Then
            a = 1
        ElseIf Mid(aa, i, 1) = "]" Then
            a = 2
        Else
            If a = 2 Then a = 0
        End If
        If a = 1 Then bb = bb & Mid(aa, i, 1)
        If a = 2 Then bb = bb & "]"
    Next iEnd SubPrivate Sub Command2_Click()'方法二
    Dim aa As String
    Dim bb As String
    Dim a() As String
    Dim b() As String
    Dim i As Integer
    Dim start As Integeraa = "[weof] (wfndo) sfon ,[ww] nosdf n[a]"
    If Left(aa, 1) = "[" Then start = 1
    a = Split(aa, "[")For i = start To UBound(a)
        b = Split(a(i), "]")
        bb = bb & "[" & b(0) & "]"
    Next i
    MsgBox bbEnd Sub
      

  4.   

    若要替换,用RegExp的Replace方法直接替换就行了
    Private Sub Command1_Click()
        Dim regexpObj As New RegExp
        regexpObj.Pattern = "\[[\u4e00-\u9fa5]+\]"
        regexpObj.Global = True
        Dim matches, match, ret
        S = "[中国] (湖南)[浙江],海南、[江苏]    *(黑龙江)"
        Set matches = regexpObj.Execute(S)
    S = regexpObj.Replace(S, "")
    MsgBox S
        
        For Each match In matches
        
          ret = ret & " " & match.Value
        Next
        MsgBox ret
    End Sub
      

  5.   

    再次感谢,可是结果还是不对,我的要求如下:
    源字符串:S = "[中国] (湖南)[浙江],海南、[江苏]    *(黑龙江)"
    replace以后得到字符串:r= "[中国 [浙江][江苏]"
      

  6.   

    Private Sub Command1_Click()
        Dim regexpObj As New RegExp
        regexpObj.Pattern = "\[[\u4e00-\u9fa5]+\]"
        regexpObj.Global = True
        Dim matches, match, ret
        S = "[中国] (湖南)[浙江],海南、[江苏]    *(黑龙江)"
        Set matches = regexpObj.Execute(S)    
        For Each match In matches
        
          ret = ret & " " & match.Value
        Next
    s=ret
        MsgBox  s
    End Sub
      

  7.   

    用循环开始那个版主已经搞出来了,结果也是对的,可是我想用replace直接出来