用自定义函数替换Print语句调用

bja*_*jan 3 regex vb6 replace notepad++

有一个遗留VB6应用程序,它使用该Print语句在整个应用程序中写入日志.有超过2万次出现Print.我想在每个Print电话上写一些更多的日志信息.

可以通过用Print我自己的函数替换调用来实现.这对未来也有帮助.

一些陈述是这样的:

Print #FileNo, Tab(1); "My Text Here";
Print #FileNo, Tab(col); Txt;
Print #FileNo, Tab(100); Format(TheDate, "DDMMMYYYY") & "    " & Variable_Name & "Field : " & Format(Field, "x")
Print #FileNo, Tab(1); Format(TheDate, "x") & " - " & TheName;
Print #FileNo, String(132, "-")
Print #FileNo, Tab(6); "SOME VALUE"; "SOME MORE VALUES";
Run Code Online (Sandbox Code Playgroud)

这里;指示Print语句不要更改行并Tab指示将插入点定位到绝对列号.

问:我如何能代替Print我自己的功能,同时保留的行为Tabsemicolon

Ahm*_*eed 5

不要将单个调用分成多个调用,而应该让您的函数期望Alex建议ParamArray参数.你的功能应该是这样的:

' Remember to set the return type or change the function to a Sub.
Public Function MyPrint(fileNo As Byte, ParamArray text() As Variant) 'As SomeType
    ' Insert body here.
End Function
Run Code Online (Sandbox Code Playgroud)

现在,我们来谈谈正则表达式.要仅使用NotePad ++,我相信您需要分两步完成.

  1. 要替换方法名称(Printto MyPrint),请使用以下模式:

    Print\h+(#\w+)
    
    Run Code Online (Sandbox Code Playgroud)

    并替换为:

    MyPrint \1
    
    Run Code Online (Sandbox Code Playgroud)

    演示.

  2. 要用逗号替换分号,可以使用以下模式:

    (?:MyPrint #\w+\K,\h*|(?!^)\G\h*)([^;\r\n]+);?
    
    Run Code Online (Sandbox Code Playgroud)

    并替换为:

    , \1
    
    Run Code Online (Sandbox Code Playgroud)

    演示.

样本输入:

Print #FileNo, Tab(1); "My Text Here";
Print #FileNo, Tab(col); Txt;
Print #FileNo, Tab(100); Format(TheDate, "DDMMMYYYY") & "    " & Variable_Name & "Field : " & Format(Field, "x")
Print #FileNo, Tab(1); Format(TheDate, "x") & " - " & TheName;
Print #FileNo, String(132, "-")
Print #FileNo, Tab(6); "SOME VALUE"; "SOME MORE VALUES";

Print #FileNo, Tab(100); "First Text"; "Second Text"
Print #FileNo, "Third Text"; "Fourth Text"
Run Code Online (Sandbox Code Playgroud)

最终输出:

MyPrint #FileNo, Tab(1), "My Text Here"
MyPrint #FileNo, Tab(col), Txt
MyPrint #FileNo, Tab(100), Format(TheDate, "DDMMMYYYY") & "    " & Variable_Name & "Field : " & Format(Field, "x")
MyPrint #FileNo, Tab(1), Format(TheDate, "x") & " - " & TheName
MyPrint #FileNo, String(132, "-")
MyPrint #FileNo, Tab(6), "SOME VALUE", "SOME MORE VALUES"

MyPrint #FileNo, Tab(100), "First Text", "Second Text"
MyPrint #FileNo, "Third Text", "Fourth Text"
Run Code Online (Sandbox Code Playgroud)