假设有这样一个字符串tmpStr="123 32    32  112",如何通过正则表达式来把所有的连续空格替换为一个?谢谢

解决方案 »

  1.   

    Private Sub Command1_Click()
        Dim i As Long
        Dim txt As String
        txt = Text1.Text
        Do
            i = InStr(1, txt, Space(2))
            If i = 0 Then Exit Do
            txt = Replace(txt, Space(2), Space(1))
        Loop    
        Text1 = txt
    End Sub
      

  2.   

    使用 \s+  =>  '?'测试页面:
    http://www.regexlab.com/zh/replshop.asp?pat=%5Cs%2B&rto=%3F&txt=123%2032%20%20%20%2032%20%20112
      

  3.   

    tmpStr="123 32    32  112"While Instr(1, tmpStr, "  ")
        tmpStr = Replace(tmpStr, "  ", " ")
    Wend
      

  4.   

    \x20{2,}        =>        \x20
      

  5.   

    sswater(光杆兵)想法不错,试一下吧
      

  6.   

    Private Sub Command1_Click()
    Dim Reg As New RegExp
    Dim strSource As String, strReplace As String
    Dim strPattern As String
    strSource = "4324 432     432  432  432432   423     432  324 32 432 4 32"
    strReplace = " " '这里是一个空格
    strPattern = "\x20{2,}"
    Reg.Pattern = strPattern
    While Reg.Test(strSource) = True
    strSource = Reg.Replace(strSource, strReplace)
    Wend
    Set Reg = Nothing
    MsgBox strSource
    End Sub-------
    www.vicmiao.com
    努力就有美好时光!