首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Regex解析CIL代码

用Regex解析CIL代码
EN

Stack Overflow用户
提问于 2012-10-21 15:50:33
回答 1查看 415关注 0票数 1

我有一个*.il文件。我想找到它中的所有非空方法(.method)。例如:

代码语言:javascript
复制
.class private auto ansi beforefieldinit MyApp.Program
       extends [mscorlib]System.Object
{
   //catch its body
  .method private hidebysig static void  Main(string[] args) cil managed
  {
    .entrypoint
    // 
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ret
  }  

  //catch its body
  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  }  

   //don't touch, it's empty
   .method public hidebysig newslot virtual 
          instance string  Invoke(string a) runtime managed
  {
  }  
 //......................................
}

现在我用类字符串来做这件事。这是相当不理性的。我试过使用Regex,但是我想不出如何创建reg表达式来捕获

  • 方法(而不是类)
  • 只有非空体的方法

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-21 16:02:15

不建议使用regex解析结构代码,这是不好的做法。

尝试使用regex模式

代码语言:javascript
复制
(\.method\s[^{]+?)(?=\s*{)(?!\s*{\s*})

测试它这里

要捕获每个方法的主体{...},请使用regex模式。

代码语言:javascript
复制
(\.method\s[^{]+{(?!\s*}).*?})

测试它这里

要了解有关正则表达式的更多信息,请访问regular-expressions.info

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12999422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档