Search code examples
c#regexcil

Parse CIL code with Regex


I have a *.il file. I want to find all non-empty methods in it (.method). For example:

.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
  {
  }  
 //......................................
}

Now I'm doing that using class string. It's quite nonrational. I tried using Regex but I could not figure out how to create a reg expression to catch only

  • methods (and not classes)
  • only methods with non-empty body

Does anybody can help me?


Solution

  • Using regex for parsing structure code is not recommended and it is bad practice

    Try to use regex pattern

    (\.method\s[^{]+?)(?=\s*{)(?!\s*{\s*})
    

    Test it here.

    To catch also the body {...} of each method, use regex pattern

    (\.method\s[^{]+{(?!\s*}).*?})
    

    Test it here.


    To learn more about regular expressions visit regular-expressions.info