python - 匹配C结构的正则表达式

标签 python c regex

我想要一个正则表达式来匹配 C 结构定义。这是我的目标数据:

typedef struct
{
}dontMatchThis;

typedef struct
{
  union //lets have a union as well
  {
    struct 
    {
     int a
     //a comment for fun

     int b;
     int c;
    };
    char byte[10];
  };
}structA;

我只想匹配 structA 的定义,从 typedef 到 strunctA。

我试过: typedef[\s\S]+?structA

但是事件虽然我使用的是非贪婪修饰符,但它匹配两个结构。 有什么建议

最佳答案

一般情况下,根本不可能。 typedefstruct 可能是由预处理器宏调用生成的(您可以在一个文件中包含 typedef,而 struct 在另一个 #include-d 文件中,或者 struct 来自一个预处理器宏,而 typedef 来自另一个。)。 p>

我建议改为通过插件或 MELT 扩展或自定义 GCC 编译器。扩展(MELT 是扩展 GCC 的领域特定语言)。

另见 etags

关于python - 匹配C结构的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16485613/

相关文章:

php - CodeIgniter 路由

Python re.sub 不返回匹配项

python - 到 Flask 的环回访问 token

python - Numpy卷积(convolve)似乎在复杂信号上产生了巨大的误差

python - 发布到 Facebook 墙

c - 如何理解“"main function' s prototype cannot provided by the program”?

c - 绕 Y 轴旋转 gluLookAt

python - 组合 __setattr__ 和 __getattr__ 会导致无限循环

c - C 编程中的子串

Javascript正则表达式-包含列表中任意顺序的单词和单词