[英]How to do a sub regular-expression replacing in each matched groups of “main” regular-expression search?
I'm working on something about hooking an existing lib. 我正在研究有关挂钩现有库的问题。 The implementation details are irrelevant to my question. 实施细节与我的问题无关。 In short, it is necessary for me to write a function definition for each original function in the lib like following: 简而言之,我有必要为lib中的每个原始函数编写一个函数定义,如下所示:
void Hooked_OriginalFunctionName(int arg0, bool arg1, const char* arg2) {
OriginalFunctionName(arg0,arg1,arg2);
//Do other things:
}
Apparently it's a tedious work of manually writing headers(codes above "//Do other things" comment) of these functions, thus regular expression replace would be a better choice: I can just copy and paste original function declarations: 显然,这是手动编写这些函数的标头(“ //执行其他操作”注释上方的代码)的繁琐工作,因此正则表达式替换将是一个更好的选择:我可以复制并粘贴原始函数声明:
void OriginalFunction0(int arg0);
void OriginalFunction1(int arg0, bool arg1);
void OriginalFunction2(const char* arg0, int arg1);
... ...
And use regular expression to replace text which matches pattern: 并使用正则表达式替换与模式匹配的文本:
void\s(\w+)\((.+)\);
with: 与:
void Hooked_$1($2) {$1($2);//Do other things}
This approach can generate codes like: 这种方法可以生成如下代码:
void Hook_OriginalFunction2(const char* arg0, int arg1) {
OriginalFunction2(const char* arg0, int arg1);
//Do other things
}
However, formats of parameter list of the function calls are obviously wrong. 但是,函数调用的参数列表格式显然是错误的。 So how can I subsequently replace 那我该如何替换
"const char* arg0, int arg1"
with 与
"arg0, arg1"
? ? Is it possible to do all these by only 1 regular expression replacement? 是否可以仅通过1个正则表达式替换来完成所有这些操作?
This perl script seems to do what you need. 这个perl脚本似乎可以满足您的需求。 It uses the negative look-behind assertion to recognise what to remove from the list of attributes: non-whitespace not ending in a comma or right parenthesis, followed by a space. 它使用否定的后向断言来识别要从属性列表中删除的内容:非空白不以逗号或右括号结尾,后跟一个空格。
#! /usr/bin/perl
use warnings;
use strict;
while (<DATA>) {
if (my ($func, $args) = /void (\w+)\((.+)\);/) {
print "void Hooked_$func($args) {\n";
$args =~ s/\S+(?<![,)]) //g;
print " $func($args);\n";
}
}
__DATA__
void OriginalFunction0(int arg0);
void OriginalFunction1(int arg0, bool arg1);
void OriginalFunction2(const char* arg0, int arg1);
void OriginalFunctionName(int arg0, bool arg1, const char* arg2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.