有一个遗留的 VB6 应用程序使用 Print
语句在整个应用程序中写入日志。 Print
的出现次数超过 2 万次。我想在每个 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";
此处 ;
指示 Print 语句不更改行,Tab
指示将插入点定位到绝对列号。
问题如何用我自己的函数替换Print
,同时保留Tab
和semicolon
的行为>?
最佳答案
与其将单个调用分解为多个调用,不如让您的函数期待一个 ParamArray
参数 as suggested by Alex .您的函数应如下所示:
' 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
现在,我们来谈谈正则表达式。要仅使用 NotePad++,我相信您需要分两步完成。
要替换方法名称(
Print
到MyPrint
),请使用以下模式:Print\h+(#\w+)
并替换为:
MyPrint \1
Demo .
要用逗号替换分号,您可以使用以下模式:
(?:MyPrint #\w+\K,\h*|(?!^)\G\h*)([^;\r\n]+);?
并替换为:
, \1
Demo .
示例输入:
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"
最终输出:
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"
关于regex - 用自定义函数替换 Print 语句调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54903789/