需要捕获模式并替换文件VBScripts中的代码

时间:2017-04-18 02:31:19

标签: regex vbscript filesystemobject

文件abc中的代码需要使用Regex捕获。

With TeWindow("tewindow").Tescreen("something").TeField("some")
 .set "value"
 .setToProperty "V"
 .exist(0)
End With

此代码应在abc中替换为

'With TeWindow("tewindow").Tescreen("something").TeField("some")
myset("something_some"), "value"
mysetToProperty("something_some"), ""
myExist("something_some"), (0)
'End With

以下是迄今为止的试验。我无法将其写入文件中。

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set testfile = objFSO.OpenTextFile("D:\test\testout4.txt", 1, True)
line = testfile.ReadAll
testfile.Close

sString = line

pat = "with[\s]{1,}tewindow\((.*?)\).tescreen\((.*?)\).tefield\((.*?)\)"  '12
pat1 = "^\.[a-zA-Z]{1,}"

Call DeclareRegEx(objRE,pat)

If objRE.test(sString) Then
  Set Matches = objRE.Execute(sString)
  Set match = Matches(0)

  intcount = match.SubMatches.Count

  If intcount > 0 Then
    For I = 1 To intcount-1
      'If i = intcount-1 Then
      objRef = objRef & match.SubMatches(I)
    Next
  Else  '30
    objRef = objRef & match.SubMatches(I) & "_"
  End If
End If

call DeclareRegEx(objRE1, pat1)
If objRE1.Test(sString) Then
  Set Matches1 = objRE1.Execute(sString)
  For Each Match1 in Matches1
    RetStr1 = Match1.Value
    strplc = Right(RetStr1, Len(RetStr1) - 1)
    actual =  objRE1.Replace(RetStr1, "my" & strplc & "(" & objRef & ")")

    MsgBox actual
  Next
End If

Function DeclareRegEx(obj, pattern)
  Set obj = New RegExp
  obj.Global = True
  obj.Multiline = True
  obj.Pattern = pattern
  obj.IgnoreCase = True
End Function

欢迎对其他方法或正则表达式提出建议。

1 个答案:

答案 0 :(得分:0)

以及找到块的方法,被详细的正则表达式捕获似乎不是代码中的通用,我尝试了类似下面的内容..

  1. 将文件内容转换为数组 2.找到与行结束的行号 3.运行一个循环,在结束之前从with的下一行迭代函数。
  2. 它对我有用!