本文介绍了使用Regex解析代码-如何使用额外的{...}捕获方法的主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 此问题来自使用Regex解析CIL代码 为了捕获方法的主体,我添加了方括号(),它变成 var regex3 = @(\.method\s [^ {] +({(?! \s *})。*?}))); ,效果很好。例如, capture.Groups [2] 给我and it worked fine. For example, capture.Groups[2] gives me { .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 }这就是我想要的。但是,如果我有and it's what I'm looking for. However if I have .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 } 然后它无法正常工作。我只是因为方法中的} ..} 而捕获了方法主体的 部分then it does not working well. I just captures the part of method's body because of } .. } within a method{ // .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 }即使包含许多{..},我如何更改正则表达式以捕获所有方法的主体?How do I change regex to be able to capture all method's body even when it contains many { .. } ?推荐答案基本来说,正则表达式不是匹配嵌套结构的正确工具,但是在您的情况下,您可以使用{。*}之类的东西来匹配所有内容,直到最后一个} (显然,这无法使用多种方法。)Basically Regexes are not the right tool for matching nested structures, however in your case you could use something like {.*} to match everything until the last } (Obviously that won't work with multiple methods.)编写CF语法解析器可以自己使用,也可以使用Antlr之类的东西。Write a CF Grammar parser yourself or use something like Antlr. 这篇关于使用Regex解析代码-如何使用额外的{...}捕获方法的主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 01:18