简体   繁体   English

使用Python和正则表达式删除数组初始化代码

[英]Remove array initialization code using Python and regular expressions

I have a .h file that is automatically generated. 我有一个自动生成的.h文件。 In this .h file there are arrays that are initialized in the following manner. 在此.h文件中,存在以以下方式初始化的数组。

char variable_1[1] = { 0 };
char variable_2[16] = 
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char variable_3[128] = 
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0};


char variable_4[63] = 
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
char variable_5[1] = { 0 };
char variable_6[32] = 
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

I am using the following regex in Python: 我在Python中使用以下正则表达式:

^char\s+variable_\w+\[\w*\]\s*=\s*\{\s*[0,]*\s*\};

This regex will only find variable_1, variable_2, variable_5 and variable_6. 此正则表达式将仅找到变量_1,变量_2,变量_5和变量_6。 However I need find all instances of these arrays and remove the initialization and leave the array names. 但是,我需要找到这些数组的所有实例,并删除初始化并保留数组名称。

char variable_1[1];
char variable_2[16];
char variable_3[128];
char variable_4[63];
char variable_5[1];
char variable_6[32];

Is there a way to accomplish this in Python? 有没有办法用Python做到这一点?

You're not checking for whitespace inside the arrays. 您不检查数组内部的空格。 Try: 尝试:

^char\\s+variable_\\w+\\[\\w*\\]\\s*=\\s*\\{\\s*[0,\\s]*\\s*\\};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM