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

标签 regex replace vb6 notepad++

有一个遗留的 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,同时保留Tabsemicolon的行为>?

最佳答案

与其将单个调用分解为多个调用,不如让您的函数期待一个 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++,我相信您需要分两步完成。

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

    Print\h+(#\w+)
    

    并替换为:

    MyPrint \1
    

    Demo .

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

    (?: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/

相关文章:

javascript - 为什么 "."没有被正则表达式捕获?

php codeigniter事件记录替换功能

R 循环用于用行模式替换 NA

vb.net - 运算符 '-' 未定义 VB6 到 VB.NET

c# - 正则表达式向前看或向后看

java - 恰好 n 次 - 组

java - IntelliJ 替换多行变量名

c++ - 如何将正错误代码从 ATL 返回到 VB6?

Windows XP 上的 Vb6 http post 请求

java - 简单英语的正则表达式