[英]Find first match following another string, python regex multiline
I know many flavors of these questions have been asked, but I am having a bit of trouble getting a regex statement working to address my specific problem. 我知道已经问过许多这样的问题,但是让正则表达式声明来解决我的特定问题有点麻烦。
I have a large number of functions with different names but the exact same format and I need to find the first match following a specific function name. 我有很多名称不同但功能完全相同的函数,我需要在特定函数名称后找到第一个匹配项。
Note that I am searching through a C file using python. 请注意,我正在使用python搜索C文件。
writecwp_positionStatus(int action,
u_char *var_val,
u_char var_val_type,
size_t var_val_len,
u_char *statP,
oid *name,
size_t name_len) {
static long intval;
static long old_intval;
switch ( action ) {
case RESERVE1:
if (var_val_type != ASN_INTEGER) {
fprintf(stderr, "write to mib not ASN_INTEGER\n");
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(long)) {
fprintf(stderr,"write to mib: bad length\n");
return SNMP_ERR_WRONGLENGTH;
}
intval = *((long *) var_val);
break;
case RESERVE2:
break;
case FREE:
/* Release any resources that have been allocated */
break;
case ACTION:
/*
* The variable has been stored in 'value' for you to use,
* and you have just been asked to do something with it.
* Note that anything done here must be reversable in the UNDO case
*/
old_intval = starting_int;
starting_int = intval;
break;
case UNDO:
/* Back out any changes made in the ACTION case */
starting_int = old_intval;
break;
case COMMIT:
/*
* Things are working well, so it's now safe to make the change
* permanently. Make sure that anything done here can't fail!
*/
break;
} return SNMP_ERR_NOERROR;
} }
In this example I want to find the first "old_intval = starting_int;" 在此示例中,我想找到第一个“ old_intval = starting_int;”。 following the function name "writecwp_positionStatus". 在函数名称“ writecwp_positionStatus”之后。 There will be many more functions with the same exact body but different names. 具有相同确切主体但名称不同的更多功能。
My thought was to set up a capture group to match: 我的想法是建立一个匹配的捕获小组:
(function name)(everything in between including newlines)(line to replace)
I tried a bunch of different options such as, but seem to be off by just a little each time: 我尝试了很多不同的选择,例如,但每次似乎只有一点点偏离:
(writecwp_positionStatus\(.*\s)((.*\s)*?)(\s*old_intval = starting_int;)
I would suggest this regex instead. 我建议改用此正则表达式。
(writecwp_positionStatus[\s\S]*?)old_intval = starting_int;([\s\S]*)
Here, the approach is to capture everything from the function-name to the statement to be repalaced by capture group 01 and then match everything after the staement by capture group 02 在这里,方法是捕获从函数名到要由捕获组01替换的语句的所有内容,然后在捕获组02的表尾匹配所有内容。
\s -> whitespace character (a space, a tab, a line break, or a form feed).
\S -> non-white space character.
*? -> ? after quantifiers makes them lazy/non-greedy.
Now to replace the statement, we can use another regex: 现在要替换该语句,我们可以使用另一个正则表达式:
\1 >>>I am the replacement<<< \2
Here, 这里,
\1 -> Everything before the statement.
\2 -> Everything after the statement.
To understand better, do experiments here . 为了更好地理解,请在此处进行实验。 I hope this is what you wanted. 希望这就是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.