【发布时间】:2022-01-24 01:38:14
【问题描述】:
我正在编写一个 python 脚本,它将帮助我使用 re.sub 功能替换我的 c++ 应用程序中的日志框架。
旧语法如下所示:
old_log_info("this is an integer: %i, this is a double: %d", 1, 2.0);
old_log_error("this is an integer: %i, this is a double: %d", 1, 2.0);
新语法:
new_log_inf("this is an integer: {}, this is a double: {}", 1, 2.0);
new_log_err("this is an integer: {}, this is a double: {}", 1, 2.0);
它也必须适用于多行语句,即:
old_log_info(
"this is an integer: %i, this is a double: %d",
1,
2.0);
应该变成:
new_log_inf(
"this is an integer: {}, this is a double: {}",
1,
2.0);
替换函数名称很简单,但替换格式说明符(%i、%d 等)仅如果出现在日志表达式中则不是。 %i 在:
printf("this is an integer: %i", 1); 应该保持不变。
我尝试使用环视来隔离old_log_info( 和最近的); 之间的子字符串:
re.sub(r'(?s)(?<=old_log_info)(?=\);)', '{}', code)
但我不知道如何只替换该匹配中的格式说明符而不是整个匹配。
【问题讨论】:
-
两个问题,你用什么日志功能?你知道你想要替换的日志行的文本吗?我猜你的例子只是一个例子,实际上并不是你要替换的。
-
你能用正则表达式展示你一直在玩什么吗,否则你可能不在正确的地方。
-
这是一个工作项目,我们正在使用 2 个不同的内部框架,我们想用薄包装的 spdlog 替换它们,但这真的无关紧要 - 例子很详尽 - 如果你发现一个“日志记录功能”然后用花括号替换调用中的所有格式说明符,并且不执行任何其他操作。我已经制作了新接口,只需更改函数名称并替换其参数中的格式说明符即可完成工作。