正则表达式在替换过程中添加不必要的空间

时间:2019-03-03 13:03:15

标签: regex vbscript wildcard

我目前在编写一个VBScript时遇到困难,该VBScript包含多个读取内容并从文本文件中替换。我正在使用的表达式查找并替换了该表达式,但是在后缀后添加了三个制表符空格,使它下面的原始行弄乱了格式。这是我在说什么的图片:

这里是前后的粘贴框,而不是图像: https://pastebin.com/Uw3H59QK

这是我的RegExp代码:

Set fso = CreateObject("Scripting.FileSystemObject")

Dim strPath

strPath = SelectFolder( "" )

If strPath = vbNull Then
    WScript.Echo "Script Cancelled - No files have been modified" 'if the user cancels the open folder dialog
Else
    WScript.Echo "Selected Folder: """ & strPath & """" 'prompt that tells you the folder you selected
End If

Function SelectFolder( myStartFolder )

    Dim objFolder, objItem, objShell
    Dim objFolderItems

    On Error Resume Next
    SelectFolder = vbNull

    Set objShell = CreateObject( "Shell.Application" )
    Set objFolder = objShell.BrowseForFolder( 0, "Please select the .dat file location folder", 0, myStartFolder)
    set objFolderItems = objFolder.Items
    If IsObject( objFolder ) Then SelectFolder = objFolder.Self.Path

    Set objFolder = Nothing
    Set objShell = Nothing

    On Error Goto 0

End Function

Set re = New RegExp 'Replacing Position Lines
re.Pattern = "Pos = \((.*)\)"
re.Global = True
re.IgnoreCase = True
For Each f in fso.GetFolder(strPath).Files
    If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
        text = f.OpenAsTextStream.ReadAll 'reading the text file
        f.OpenAsTextStream(2).Write re.Replace(text, """Position"" : mathutils.Vector(($1)),")
        count = count + 1
    End If
Next

Set reAngles = New RegExp 'Replacing Angles
reAngles.Pattern = "Angles = \((.*)\)"
reAngles.Global = True
reAngles.IgnoreCase = True
For Each f in fso.GetFolder(strPath).files
  If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
    text = f.OpenAsTextStream.ReadAll
    f.OpenAsTextStream(2).Write reAngles.Replace(text, """Angles"" : mathutils.Vector(($1)),")
  End If
Next

Set reNames = New RegExp 'Replacing Names
reNames.Pattern = "Name = (.*)"
reNames.Global = True
'reNames.Multiline = True
reNames.IgnoreCase = True
For Each f in fso.GetFolder(strPath).files
  If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
    text = f.OpenAsTextStream.ReadAll
    f.OpenAsTextStream(2).Write reNames.Replace(text, """Name"" : ""$1"",")
  End If
Next

我最好的猜测是通配符正在获取比所需更多的信息.​​..但是我不确定如何解决该问题。我在Notepad ++中使用了很多这样的表达式,因此希望将它们轻松转换为VBS!

0 个答案:

没有答案
相关问题