我正在研究一个旧的 C 代码,其中有如下调试语句:
debug(1, "SomeDebugStmt %s %d", someString, someNumber);
...
debug(2, "another SomeDebugStmt number 2 %s %d", anotherString, anotherNumber);
这个 C 文件中有数百个这样的调试语句。
我怎样才能将此类调试语句更改为这种格式:
debug(1, "%s--[%d] SomeDebugStmt %s %d", fname, line, someSTring, someNumber);
...
debug(2, "%s--[%d] another SomeDebugStmt number 2 %s %d", fname, line, someSTring, anotherString, anotherNumber);
我以为使用正则表达式查找和替换可以做到这一点,但我不确定它是否能以某种方式记住替换原始字符串中的精确字符串,同时添加一些额外的字符串值。我可以选择使用 Eclispe 或 Notepad++。任何提示/指针都值得赞赏。:)
答案1
您可以使用 notepad++ 并使用以下 RegExp 来执行“查找内容:”
debug\(([0-9]+,) "
并替换为
debug(\1 "%s--[%d] "
正则表达式的解释
debug ; find the text "debug"
\( ; followed by a opening parenthesis,
; parentheses have a special meaning in regexp,
; so they must be escaped with a backslash
( ; followed by the first subpattern, that will go in backreference \1
[0-9]+ ; one or more digits
, ; followed by a comma
) ; end of the subpattern
" ; followed by a blank and a double quote
我们用以下代码替换它
debug( ; exact this text
\1 ; followed by the content of the first subpattern
" ; the blank and the double quote
%s--[%d] " ; and the text you want to insert in those lines