Regex for matching method contents

To develop a Java Code parser, I want to extract the contents of a method from java source file as strings. But the problem is that I can not compare the contents of the Regex, giving a number between {and }because some of the techniques are {and }inside the method. Like this,

   public String[] getArgs() {

       try{
          //something
       }
       catch(Exception e){

       }
     return args;
   }

If I use regex like

Regex regex = new Regex("(?<={).*?(?=})");

He only captures try{ //something

How can I ignore the occurrences of a method {both }inside and get the value inside the method, e.g.

try{
      //something
   }
   catch(Exception e){

   }
 return args;
+5
source share
2 answers

#. , {}. : http://www.codeproject.com/KB/recipes/Nested_RegEx_explained.aspx

var reg = @"
(?<body>
\{(?<DEPTH>)
(?>
(?<DEPTH>)\{
    |
\}(?<-DEPTH>)  
    |
(?(DEPTH)[^\{\}]* | )
)*
\}(?<-DEPTH>)
(?(DEPTH)(?!))
)";
        var input = "abc{d{e}f}gh{i}";
        foreach (Match m in Regex.Matches(input,reg, RegexOptions.IgnorePatternWhitespace)) Console.WriteLine(m.Groups["body"].Value);

[] , "RegexOptions.IgnorePatternWhitespace"

:

{ {} } ​​

{}

+2

, , . , , , .

, . Java.

0

All Articles