Results 1 to 27 of 27

Thread: [RESOLVED] [VB6] - separe all words and symbols

Hybrid View

  1. #1
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: [RESOLVED] [VB6] - separe all words and symbols

    Does this do it?
    Code:
    Private Function ExtendedSplit(ByVal strInputString As String) As String()
    Dim i As Integer
    Dim objRegExp
    Dim objMatch
    Dim colMatches
    Dim strTestInput As String
    Dim strOldValue As String
    Dim strNewValue As String
    Dim tempArr() As String
        
        ' Create a regular expression object.
        Set objRegExp = CreateObject("VBScript.RegExp")
        
        'Set the pattern.
        objRegExp.Pattern = "\"".*?\"""
        
        'Set global applicability.
        objRegExp.Global = True
        
        'Test whether the String can be compared.
        If (objRegExp.Test(strInputString) = True) Then
            'Get the matches.
            Set colMatches = objRegExp.Execute(strInputString)
            
            Debug.Print strInputString
            
            ' Iterate Matches collection.
            For Each objMatch In colMatches
                strOldValue = objMatch.Value
                strNewValue = Replace(strOldValue, Chr(34), Chr(128))
                strNewValue = Replace(strNewValue, Chr(32), Chr(129))
                'Debug.Print objMatch.Value
                
                strInputString = Replace(strInputString, strOldValue, strNewValue)
            Next
            
            Debug.Print strInputString
        End If
        
        For i = 1 To 127
            Select Case i
                Case 1 To 47, 59 To 64, 91 To 96, 123 To 127
                    strInputString = Replace(strInputString, Chr(i), " " & Chr(i) & " ")
            End Select
        Next i
        
        Do While InStr(strInputString, Space(2))
            strInputString = Replace(strInputString, Space(2), Space(1))
        Loop
        
        strInputString = Trim(strInputString)
        tempArr = Split(strInputString, " ")
        
        For i = 0 To UBound(tempArr)
            tempArr(i) = Replace(tempArr(i), Chr(128), Chr(34))
            tempArr(i) = Replace(tempArr(i), Chr(129), Chr(32))
        Next i
        
        ExtendedSplit = tempArr
    End Function

  2. #2

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [RESOLVED] [VB6] - separe all words and symbols

    Quote Originally Posted by MarkT View Post
    Does this do it?
    Code:
    Private Function ExtendedSplit(ByVal strInputString As String) As String()
    Dim i As Integer
    Dim objRegExp
    Dim objMatch
    Dim colMatches
    Dim strTestInput As String
    Dim strOldValue As String
    Dim strNewValue As String
    Dim tempArr() As String
        
        ' Create a regular expression object.
        Set objRegExp = CreateObject("VBScript.RegExp")
        
        'Set the pattern.
        objRegExp.Pattern = "\"".*?\"""
        
        'Set global applicability.
        objRegExp.Global = True
        
        'Test whether the String can be compared.
        If (objRegExp.Test(strInputString) = True) Then
            'Get the matches.
            Set colMatches = objRegExp.Execute(strInputString)
            
            Debug.Print strInputString
            
            ' Iterate Matches collection.
            For Each objMatch In colMatches
                strOldValue = objMatch.Value
                strNewValue = Replace(strOldValue, Chr(34), Chr(128))
                strNewValue = Replace(strNewValue, Chr(32), Chr(129))
                'Debug.Print objMatch.Value
                
                strInputString = Replace(strInputString, strOldValue, strNewValue)
            Next
            
            Debug.Print strInputString
        End If
        
        For i = 1 To 127
            Select Case i
                Case 1 To 47, 59 To 64, 91 To 96, 123 To 127
                    strInputString = Replace(strInputString, Chr(i), " " & Chr(i) & " ")
            End Select
        Next i
        
        Do While InStr(strInputString, Space(2))
            strInputString = Replace(strInputString, Space(2), Space(1))
        Loop
        
        strInputString = Trim(strInputString)
        tempArr = Split(strInputString, " ")
        
        For i = 0 To UBound(tempArr)
            tempArr(i) = Replace(tempArr(i), Chr(128), Chr(34))
            tempArr(i) = Replace(tempArr(i), Chr(129), Chr(32))
        Next i
        
        ExtendedSplit = tempArr
    End Function
    yes. .thanks for all.. realy.. thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: [RESOLVED] [VB6] - separe all words and symbols

    I couldn't resist
    I'm pretty sure a lot of people see the usage in parsing Command-Line-Arguments

    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function StrSpn Lib "SHLWAPI" Alias "StrSpnW" (ByVal psz As Long, ByVal pszSet As Long) As Long
    Private Declare Function StrCSpn Lib "SHLWAPI" Alias "StrCSpnW" (ByVal lpStr As Long, ByVal lpSet As Long) As Long
    
    Private Const sEmpty = ""
    Private Const sQuote2 = """"
    Private Const chQuote = 34  ' Asc("""")
    Private Const Separator As String = "();, +-*/" & sQuote2
    'private const MyString As String = "int soma(int a,int b)"
    Private Const MyString As String = "write(""Hello World"")"
    
    Sub main()
    Dim Result As String
        
        Result = GetQToken(MyString, Separator)
        
        Debug.Print Result
        
        Do
            'second and consecutive calls with empty string
            Result = GetQToken(sEmpty, Separator)
            
            If Result <> " " And Result <> sEmpty Then Debug.Print Result
        
        Loop Until Result = ""
        
    
    End Sub
    
    ' New GetQToken uses faster StrSpn and StrCSpn from SHLWAPI.DLL
    Function GetQToken(sTarget As String, sSeps As String) As String
        ' GetQToken = sEmpty
        
        
        ' Note that sSave, pSave, pCur, and cSave must be static between calls
        Static sSave As String, pSave As Long, pCur As Long, cSave As Long
        ' First time through save start and length of string
        If sTarget <> sEmpty Then
            ' Save in case sTarget is moveable string (Command$)
            sSave = sTarget
            pSave = StrPtr(sSave)
            pCur = pSave
            cSave = Len(sSave)
        Else
            ' Quit if past end (also catches null or empty target)
            If pCur >= pSave + (cSave * 2) Then Exit Function
        End If
        
        Dim pSeps As Long
        pSeps = StrPtr(sSeps)
    
        ' Get current character
        Dim c As Long
        ' Find start of next token
        c = StrSpn(pCur, pSeps)
        ' Set position to start of token
        If c Then
                'catching the separators
                GetQToken = String$(1, 0)
                CopyMemory ByVal StrPtr(GetQToken), ByVal pCur, 2
                pCur = pCur + 2
                Exit Function
            
        End If
        
        Dim ch As Integer
        CopyMemory ch, ByVal pCur - 2, 2
        ' Check first character for quote, then find end of token
        If ch = chQuote Then
            c = StrCSpn(pCur, StrPtr(sQuote2))
        Else
            c = StrCSpn(pCur, pSeps)
        End If
        ' If token length is zero, we're at end
        If c = 0 Then Exit Function
        
        ' Cut token out of target string
        GetQToken = String$(c, 0)
        CopyMemory ByVal StrPtr(GetQToken), ByVal pCur, c * 2
        ' Set new starting position
        pCur = pCur + (c * 2)
    
    End Function
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [RESOLVED] [VB6] - separe all words and symbols

    Quote Originally Posted by Zvoni View Post
    I couldn't resist
    I'm pretty sure a lot of people see the usage in parsing Command-Line-Arguments

    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function StrSpn Lib "SHLWAPI" Alias "StrSpnW" (ByVal psz As Long, ByVal pszSet As Long) As Long
    Private Declare Function StrCSpn Lib "SHLWAPI" Alias "StrCSpnW" (ByVal lpStr As Long, ByVal lpSet As Long) As Long
    
    Private Const sEmpty = ""
    Private Const sQuote2 = """"
    Private Const chQuote = 34  ' Asc("""")
    Private Const Separator As String = "();, +-*/" & sQuote2
    'private const MyString As String = "int soma(int a,int b)"
    Private Const MyString As String = "write(""Hello World"")"
    
    Sub main()
    Dim Result As String
        
        Result = GetQToken(MyString, Separator)
        
        Debug.Print Result
        
        Do
            'second and consecutive calls with empty string
            Result = GetQToken(sEmpty, Separator)
            
            If Result <> " " And Result <> sEmpty Then Debug.Print Result
        
        Loop Until Result = ""
        
    
    End Sub
    
    ' New GetQToken uses faster StrSpn and StrCSpn from SHLWAPI.DLL
    Function GetQToken(sTarget As String, sSeps As String) As String
        ' GetQToken = sEmpty
        
        
        ' Note that sSave, pSave, pCur, and cSave must be static between calls
        Static sSave As String, pSave As Long, pCur As Long, cSave As Long
        ' First time through save start and length of string
        If sTarget <> sEmpty Then
            ' Save in case sTarget is moveable string (Command$)
            sSave = sTarget
            pSave = StrPtr(sSave)
            pCur = pSave
            cSave = Len(sSave)
        Else
            ' Quit if past end (also catches null or empty target)
            If pCur >= pSave + (cSave * 2) Then Exit Function
        End If
        
        Dim pSeps As Long
        pSeps = StrPtr(sSeps)
    
        ' Get current character
        Dim c As Long
        ' Find start of next token
        c = StrSpn(pCur, pSeps)
        ' Set position to start of token
        If c Then
                'catching the separators
                GetQToken = String$(1, 0)
                CopyMemory ByVal StrPtr(GetQToken), ByVal pCur, 2
                pCur = pCur + 2
                Exit Function
            
        End If
        
        Dim ch As Integer
        CopyMemory ch, ByVal pCur - 2, 2
        ' Check first character for quote, then find end of token
        If ch = chQuote Then
            c = StrCSpn(pCur, StrPtr(sQuote2))
        Else
            c = StrCSpn(pCur, pSeps)
        End If
        ' If token length is zero, we're at end
        If c = 0 Then Exit Function
        
        ' Cut token out of target string
        GetQToken = String$(c, 0)
        CopyMemory ByVal StrPtr(GetQToken), ByVal pCur, c * 2
        ' Set new starting position
        pCur = pCur + (c * 2)
    
    End Function
    thanks for share
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width