【问题标题】:Delete special character SOH with RegExp使用 RegExp 删除特殊字符 SOH
【发布时间】:2020-01-07 18:07:33
【问题描述】:

对于 Access 开发,我使用了 GetOpenFilename 函数,如 here 所述 但是 lpstrTitle 是用特殊字符 SOH 设置的。为了删除 SOH,我创建了一个 regExp 函数

Public Function RegParse(sStr As String) '----> sStr is lpstrTitle from getopenfiled 

Dim oRegex As New RegExp

sPattern = "^.*?(?=\x01)"  '--> Failed on .Test   
'sPattern = ^[^\x01]*  '--> successful.Test but SOH still there
'sPattern = (^.*)v(.*)  '-->Ok but  v deleted  
.Replace(sStr, "$1")

With oRegex 
    .IgnoreCase = True
    .pattern = sPattern
    .Global = False

    If .Test(sStr) Then
     sStr1 = .Execute(sStr)(0) 

End With

End Function

但是 sStr1 仍然是 SOH 字符,sPattern = ^[^\x01]*

并且命令 sStr1 = .replace(sStr1, “$1”) 是不可能的,因为 sPattern = "^.*?(?=\x01) 在 .test 中失败

提前感谢您的帮助

【问题讨论】:

    标签: vba regexp-replace


    【解决方案1】:

    如果要删除特定字符,只需进行简单的查找和替换即可:

    sStr1 = Replace(sStr1, Chr(1), "", Compare := vbBinaryCompare)
    

    vbBinaryCompare 使查找和替换二进制,以避免与控制字符的怪异。

    【讨论】:

    • 我试过 sFileName = Replace(typOpenFile.lpstrFile, Chr(1), "", Compare:=vbBinaryCompare) 但没有成功。 SOH 字符不会被删除
    • 很难相信他们是否是真正的 SOH 角色...请尝试提供minimal reproducible example
    • hu Erik,首先再次感谢您的帮助。单击此link 并上传 New1.txt 文件作为示例
    • 这对我没有帮助。我的函数能够很好地去除 SOH 字符。我确实注意到一个错字(= 应该是 :=),但答案应该可以正常工作
    • 请现场分享代码。编辑问题以演示整个问题。另外,我强烈建议使用 unicode 等效函数。 ANSI 函数往往会遇到奇怪的字符串问题。
    【解决方案2】:

    模块中的代码

    Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    
    Public Type OPENFILENAME
    lStructSize       As Long
    hwndOwner         As Long
    hInstance         As Long
    lpstrFilter       As String
    lpstrCustomFilter As String
    nMaxCustFilter    As Long
    nFilterIndex      As Long
    lpstrFile         As String
    nMaxFile          As Long
    lpstrFileTitle    As String
    nMaxFileTitle     As Long
    lpstrInitialDir   As String
    lpstrTitle        As String
    flags             As Long
    nFileOffset       As Integer
    nFileExtension    As Integer
    lpstrDefExt       As String
    lCustData         As Long
    lpfnHook          As Long
    lpTemplateName    As String
    End Type
    
    Public typOpenFile As OPENFILENAME
    
    Public Function RegParse(psStr As String, psPattern As String) As String
    
    Dim oRegex As New RegExp
    Dim sStr As String, sPattern As String
    
    sStr2 = Replace(psStr, Chr(1), "", Compare:=vbBinaryCompare)
    Debug.Print sStr2
    
    
    sPattern = psPattern
        With oRegex
        .Global = True     'restricting regex to find only first match.
        .IgnoreCase = True  'ignoring cases while regex engine performs the search.
        .Pattern = sPattern
    
        If .Test(psStr) Then              'Testing if the pattern matches or not
            sStr = .Execute(psStr)(0)     'will provide the String which matches with Regex 
            sStr1 = .Replace(psStr, "$1") '.Replace function will replace the String with whatever is in the first set of braces - $X
        End If
    End With
    
    Exit_:
    
    RegParse = sStr
    Exit Function
    
    Err_:
    sStr = ""
    gsMsg = Err.Number & " : " & Err.Description & Chr(13) & "Process aborted"
    MsgBox gsMsg, vbCritical, "Error message"
    GoTo Exit_
    
    End Function
    
    
    Public Function mfOpenFileDialog(psPathDir As String, Optional psFileCrit As String) As Boolean
    
    Dim lReturn As Long
    Dim strFilter As String
    Dim sFileSelected As String
    Dim bOk As Boolean
    
    bOk = True
    
    typOpenFile.lStructSize = Len(typOpenFile)
    strFilter = "Text File (*" & psFileCrit & "*.csv)" & Chr(0) & "*" & psFileCrit & "*.csv" & Chr(0) '--> Define your filter here
    
    With typOpenFile
        .lpstrFilter = strFilter
        .nFilterIndex = 1
        .lpstrFile = String(257, 0)
        .nMaxFile = Len(.lpstrFile) - 1
        .lpstrFileTitle = .lpstrFile
        .nMaxFileTitle = .nMaxFile
        .lpstrInitialDir = psPathDir
        .lpstrTitle = "My FileFilter Open"
        .flags = 0
    End With
    
    If GetOpenFileName(typOpenFile) = 0 Then
        MsgBox "No file selected", vbCritical, "Error message"
        bOk = False
    End If
    
    Exit_:
    
    mfOpenFileDialog = bOk
    Exit Function
    
    Err_:
    
    bOk = False
    gsMsg = "Function mfOpenFileDialog" & Chr(13) & Err.Number & Chr(13) & Err.Description
    MsgBox gsMsg, vbCritical, "Error message"
    GoTo Exit_
    
    End Function
    

    我从点击按钮事件中调用 mfOpenFileDialog 函数

    sPathDefault = "c:\Extraction"
    sFileCrit = "rapport_"
    If mfOpenFileDialog(sPathDefault, sFileCrit) = False Then GoTo Exit_
    sPattern = "(^.*?(?=\x01))(\x01*)"
    sFileName = RegParse(typOpenFile.lpstrFile, sPattern)
    

    就是这样

    【讨论】:

      【解决方案3】:

      其实特殊字符不是chr(1)而是chr(0)或者x00 我用这个功能测试过

      Public Function mfShowChar(psStr As String)
        Dim i As Integer
        Dim arrChar() As Integer
      
        For i = 1 To Len(psStr)
           ReDim Preserve arrChar(i)
           ArrChar(i) = Asc(Mid(psStr, i)) 
        Next
      
       End Function
      

      这两种解决方案都有效

      • sStr = Replace(typOpenFile.lpstrFile, Chr(0), vbNullString)
      • RegExp 但带有 sPattern = "(^.?(?=\x00))(\x00)"

      【讨论】:

        猜你喜欢
        • 2017-09-10
        • 2020-12-02
        • 2020-11-19
        • 1970-01-01
        • 1970-01-01
        • 2019-01-29
        • 2021-07-01
        • 1970-01-01
        • 2014-11-10
        相关资源
        最近更新 更多