Vbscript 在两个字符串之间替换某些字符串的实例

Vbscript 在两个字符串之间替换某些字符串的实例,vbscript,g-code,Vbscript,G Code,我试图创建一个VBScript来查找并替换位于两个字符串之间的某个字符串。以下是脚本将在其上执行的文件示例: % N1(已发布文件名-WORKNC POST) N2(输入文件名-6033_u01.TBA) N3(日期/时间:2018年3月15日星期四06:36:08) N4 G0 G40 G80 G90 G98 N5 G17 N6 G57H901 N7 G173W0.0 N8 B0.000 N9(攻丝机0.250000) N10 T21 N11 M06 N12 S100 N13 M843 N14

我试图创建一个VBScript来查找并替换位于两个字符串之间的某个字符串。以下是脚本将在其上执行的文件示例:

%
N1(已发布文件名-WORKNC POST)
N2(输入文件名-6033_u01.TBA)
N3(日期/时间:2018年3月15日星期四06:36:08)
N4 G0 G40 G80 G90 G98
N5 G17
N6 G57H901
N7 G173W0.0
N8 B0.000
N9(攻丝机0.250000)
N10 T21
N11 M06
N12 S100
N13 M843
N14 G173 W0.0
N15(-)
N16(攻丝)
N17 G0 G90 X-0.0001 Y8.8135
N18 G43 Z10.0632 H21
N19 G01 F500。X-0.0001 Y8.8135
N20 G01 Z9.7163 F500。
N21 G98 G84 X-0.0001 Y8.8135 Z6.0376 R7.6376 E10。
N22 G80 G01 F500。
N23 X-0.0001 Y8.8135 Z9.7163
N24 X-0.0001 Y8.8135 Z9.7163
N25
N26 M845
N27 G91 G28 Z0
N28 G90
N29 G57H901
N30 G173W0.0
N31 B0.000
N32(钻孔0.005000)
N33 T19
N34 M06
N35 S5000
N36 M3
N37 G173 W0.0
N38(-)
N39(轮廓倒角)
N40 G0 G90 X-0.0001 Y8.8135
N41 G43 Z9.7163 H19
N42 Z7.6375
N43 G01 Z9.8376 F500。
N44 X-0.0001 Y8.8135 Z10.0632
N45
N46 M05
N47 G91 G28 Z0
N48 G90
N49 G57H901
N50 G173W0.0
N51 B0.000
N52(攻丝机0.750000)
N53 T21
N54 M06
N55 S100
N56 M843
N57 G173 W0.0
N58(-)
N59(攻丝)
N60 G0 G90 X-0.0001 Y8.8135
N61 G43 Z10.0632 H21
N62 G01 F500。X-0.0001 Y8.8135
N63 G01 Z9.7163 F500。
N64 G98 G84 X-0.0001 Y8.8135 Z6.0376 R7.6376 E10。
N65 G98 G84 X-0.0001 Y10.8135 Z6.0376 R7.6376 E10。
N66 G80 G01 F500。
N67 X-0.0001 Y8.8135 Z9.7163
N68 X-0.0001 Y8.8135 Z9.7163
N69
N70 M845
N71 G91 G28 Z0
N72 G90
N73 M30
%
所以在这段代码中有两个攻丝操作。我正在尝试创建一个介于
Tapper 0.250000
M845
之间的脚本,并使用一个基于
Tapper 0.250000
中的数字的变量替换
E10的所有实例。

例如,假设有
Tapper 0.250000
和一些X行之后的
M845
,出现在这两者之间的
E10.
需要替换为
E0.250000
。如果有另一个攻丝操作,如
攻丝器0.750000
,则攻丝器和M845之间的
E10.
需要更换为
E0.787

以下是迄今为止我的VBScript:

strFileName = Wscript.Arguments(0)
strOutputFile = Wscript.Arguments(0)

Set fso = CreateObject("Scripting.FileSystemObject")

'Variable patterns to search for. There may be 9 of these total
strPattern25 = "(Tapper 0.250000)(.|\s)*(M845)"
strPattern375 = "(Tapper 0.375000)(.|\s)*(M845)"
strPattern5 = "(Tapper 0.500000)(.|\s)*(M845)"
strPattern75 = "(Tapper 0.750000)(.|\s)*(M845)"

strFindString = "E10." 'String to find and then replace each instance of between pattern

'Variables of the replace string
strReplaceString25 = "E0.05"
strReplaceString375 = "E0.0625"
strReplaceString5 = "E0.0769"
strReplaceString75 = "E0.787"

strTestString = fso.OpenTextFile(strFileName, 1).ReadAll 'open and read the file

'Replacing script. Currently replaces all text between the strPattern. Need to create another function in order to only replace the E10.
strNewText25 = fReplaceText(strPattern25, strTestString, strReplaceString25)
strNewText375 = fReplaceText(strPattern375, strTestString, strReplaceString375)
strNewText5 = fReplaceText(strPattern5, strTestString, strReplaceString5)
strNewText75 = fReplaceText(strPattern75, strTestString, strReplaceString75)

fso.OpenTextFile(strOutputFile, 2, True).WriteLine strNewText

MsgBox "Done!"

'Function
Function fReplaceText(sPattern, sStr, sReplStr)
    Dim regEx, oMatch, colMatches, temp
    Set regEx = New RegExp     ' Create a regular expression.
    regEx.Pattern = sPattern   ' Set pattern
    regEx.IgnoreCase = True    ' Set case insensitivity.
    regEx.Global = True        ' Set global applicability

    Set colMatches = regEx.Execute(sStr)   ' Execute search

    If colMatches.Count = 0 Then
        temp = ""
    Else
        For Each oMatch In colMatches
            temp = regEx.Replace(sStr, oMatch.SubMatches(0) & vbCrlf & sReplStr & vbCrlf & oMatch.SubMatches(2))
        Next
    End If
    fReplaceText = temp
End Function

我使用以下方法获得了期望的结果:

  • 读取文件的所有内容
  • 使用正则表达式获取以
    Tapper
    开头,以
    M845
    结尾的所有子字符串
  • 在每个子字符串中,将
    E10
    的所有实例替换为
    E
  • 将修改后的子字符串写回文件
从以下代码获取帮助:

Option Explicit
Dim strFilePath, objFso, objFile, strFileContents, objReg, objMatches, i, strTemp, strNew
strFilePath  = "C:\Users\gurmansingh\Desktop\Test\inout.txt"            'Give the appropriate file path here or use the arguments, like you are using now
Set objFso = CreateObject("scripting.filesystemobject")
Set objFile = objFso.OpenTextFile(strFilePath,1,False)
strFileContents = objFile.ReadAll                                       'Reading all the contents of the File. Note that this method of reading will prove to be a bit inefficient when the file size is large
objFile.Close

Set objReg = New RegExp
objReg.Global = True
objReg.Pattern = "Tapper\s*(\d*(?:\.\d+)?)[\s\S]*?M845"                 'Pattern Explained later
Set objMatches = objReg.Execute(strFileContents)                        'Finds all the substrings in the file that matches the above Regex Pattern
For i=0 To objMatches.Count-1
    strTemp = objMatches.Item(i).Submatches.Item(0)                     'Grabbing the digits that come after the text 'Tapper '
    strNew = Replace(objMatches.Item(i),"E10","E"&strTemp)              'Replacing all the E10's in each match
    strFileContents = Replace(strFileContents,objMatches.Item(i),strNew)
Next
Set objFile = objFso.OpenTextFile(strFilePath,2,False)
objFile.Write strFileContents
objFile.Close
Set objFile = Nothing
Set objFso = Nothing
Tapper\s*(\d*(?:\.\d+)?)[\s\S]*?M845
正则表达式解释:

Option Explicit
Dim strFilePath, objFso, objFile, strFileContents, objReg, objMatches, i, strTemp, strNew
strFilePath  = "C:\Users\gurmansingh\Desktop\Test\inout.txt"            'Give the appropriate file path here or use the arguments, like you are using now
Set objFso = CreateObject("scripting.filesystemobject")
Set objFile = objFso.OpenTextFile(strFilePath,1,False)
strFileContents = objFile.ReadAll                                       'Reading all the contents of the File. Note that this method of reading will prove to be a bit inefficient when the file size is large
objFile.Close

Set objReg = New RegExp
objReg.Global = True
objReg.Pattern = "Tapper\s*(\d*(?:\.\d+)?)[\s\S]*?M845"                 'Pattern Explained later
Set objMatches = objReg.Execute(strFileContents)                        'Finds all the substrings in the file that matches the above Regex Pattern
For i=0 To objMatches.Count-1
    strTemp = objMatches.Item(i).Submatches.Item(0)                     'Grabbing the digits that come after the text 'Tapper '
    strNew = Replace(objMatches.Item(i),"E10","E"&strTemp)              'Replacing all the E10's in each match
    strFileContents = Replace(strFileContents,objMatches.Item(i),strNew)
Next
Set objFile = objFso.OpenTextFile(strFilePath,2,False)
objFile.Write strFileContents
objFile.Close
Set objFile = Nothing
Set objFso = Nothing
Tapper\s*(\d*(?:\.\d+)?)[\s\S]*?M845
  • Tapper\s*
    -匹配文本
    Tapper
    ,后跟0+空格
  • (\d*(?:\。\d+))
    -匹配并捕获组1中的数字序列。此组中捕获的内容稍后将用于替换
    E10
  • [\s\s]*?
    -匹配任何字符的0+次出现次数,尽可能少
  • M845
    -匹配
    M845

感谢@Gurman的洞察力!以下是我最后做的:

Option Explicit
Dim strFilePath, objFso, objFile, strFileContents, objReg, objMatches, i, strTemp, strNew

strFilePath  = Wscript.Arguments(0)


Set objFso = CreateObject("scripting.filesystemobject")
Set objFile = objFso.OpenTextFile(strFilePath,1,False)
strFileContents = objFile.ReadAll  'Reading all the contents of the File
objFile.Close

Set objReg = New RegExp
objReg.Global = True
objReg.Pattern = "Tapper\s*(\d*(?:\.\d+)?)[\s\S]*?M845" 'Pattern Explained later
Set objMatches = objReg.Execute(strFileContents)        'Finds all the substrings in the file that matches the above Regex Pattern




For v=0 To objMatches.Count-1
    strTemp = objMatches.Item(v).Submatches.Item(0) 'Grabbing the digits that come after the text 'Tapper '

        if strTemp = "0.250000" then '1/4-20
            strNew = Replace(objMatches.Item(v),"E10.","F0.05") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.375000" then '3/8-16
            strNew = Replace(objMatches.Item(v),"E10.","F0.0625") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.500000" then '1/2-13
            strNew = Replace(objMatches.Item(v),"E10.","F0.0769") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.312500" then '5/16-18
            strNew = Replace(objMatches.Item(v),"E10.","F0.0556") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.472000" then 'M12X1.75
            strNew = Replace(objMatches.Item(v),"E10.","F0.0689") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.630000" then 'M16X2.00
            strNew = Replace(objMatches.Item(v),"E10.","F0.0787") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.750000" then '3/4-10
            strNew = Replace(objMatches.Item(v),"E10.","F0.1") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.625000" then '5/8-11
            strNew = Replace(objMatches.Item(v),"E10.","F0.0909") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "1.000000" then '1-8
            strNew = Replace(objMatches.Item(v),"E10.","F0.125") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        elseif strTemp = "0.960000" then 'M24X3.0
            strNew = Replace(objMatches.Item(v),"E10.","F0.118") 'New Feed Rate
            strFileContents = Replace(strFileContents,objMatches.Item(v),strNew)

        End If
Next
Set objFile = objFso.OpenTextFile(strFilePath,2,False)
objFile.Write strFileContents
objFile.Close
Set objFile = Nothing
Set objFso = Nothing
我只是浏览并创建了一组
if
语句,这些语句决定了将进给速率更改为什么。再次感谢@Gurman