使用 RegExp 删除特殊字符 SOH

Delete special character SOH with RegExp

对于 Access 开发,我按照说明使用了 GetOpenFilename 函数 here 但是 lpstrTitle 设置了特殊字符 SOH。为了删除 SOH,我创建了一个正则表达式函数

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, "")

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

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

End With

End Function

但是 sStr1 仍然带有 sPattern = ^[^\x01]*

的 SOH 字符

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

中失败

提前感谢您的帮助

如果要删除特定字符,只需执行简单的查找并替换为空即可:

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

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

我在模块中的代码

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, "") '.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)

就这些了

其实特殊字符不是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 = 替换(typOpenFile.lpstrFile, Chr(0), vbNullString)
  • RegExp 但 sPattern = "(^.?(?=\x00))(\x00)"