![在 Eclipse 或 Notepad++ 中选择性查找和替换以美化我的 C 代码](https://linux22.com/image/1409449/%E5%9C%A8%20Eclipse%20%E6%88%96%20Notepad%2B%2B%20%E4%B8%AD%E9%80%89%E6%8B%A9%E6%80%A7%E6%9F%A5%E6%89%BE%E5%92%8C%E6%9B%BF%E6%8D%A2%E4%BB%A5%E7%BE%8E%E5%8C%96%E6%88%91%E7%9A%84%20C%20%E4%BB%A3%E7%A0%81.png)
我正在研究一个旧的 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