重複の可能性:
正規表現を使用してCILコードを解析します
この質問は、正規表現を使用したCILコードの解析から来ています。
メソッドの本体をキャプチャするために、ブラケットを追加()
しました。
var regex3 = @"(\.method\s[^{]+({(?!\s*}).*?}))";
そしてそれはうまくいきました。たとえば、capture.Groups[2]
私に
{
.entrypoint
//
.maxstack 8
IL_0000: nop
IL_0001: call void TestAssemblyConsole.Test::Method1()
IL_0006: nop
IL_0007: call int32 TestAssemblyConsole.Test::Method2()
IL_000c: pop
IL_000d: call string [mscorlib]System.Console::ReadLine()
IL_0012: pop
IL_0013: ret
}
それが私が探しているものです。しかし、私が持っている場合
.method public hidebysig static void Method1() cil managed
{
//
.maxstack 3
.locals init (class [mscorlib]System.Exception V_0)
IL_0000: nop
.try
{
.try
{
IL_0001: nop
IL_0002: ldstr "gfhgfhgfhg"
IL_0007: call void [mscorlib]System.Console::WriteLine(string)
IL_000c: nop
IL_000d: nop
IL_000e: leave.s IL_0020
} // end .try
catch [mscorlib]System.Exception
{
IL_0010: stloc.0
IL_0011: nop
IL_0012: ldstr "exception"
IL_0017: call void [mscorlib]System.Console::WriteLine(string)
IL_001c: nop
IL_001d: nop
IL_001e: leave.s IL_0020
} // end handler
IL_0020: nop
IL_0021: leave.s IL_0031
} // end .try
finally
{
IL_0023: nop
IL_0024: ldstr "finally"
IL_002f: nop
IL_0030: endfinally
} // end handler
IL_0031: nop
IL_0032: ret
}
その後、それはうまく機能しません。メソッド内にあるため、メソッドの本体の一部をキャプチャするだけです} .. }
{
//
.maxstack 1
.locals init (class [mscorlib]System.Exception V_0)
IL_0000: nop
.try
{
.try
{
IL_0001: nop
IL_0002: ldstr "gfhgfhgfhg"
IL_0007: call void [mscorlib]System.Console::WriteLine(string)
IL_000c: nop
IL_000d: nop
IL_000e: leave.s IL_0020
}
多くの{..}が含まれている場合でも、すべてのメソッドの本体をキャプチャできるように正規表現を変更するにはどうすればよいですか?