Nipuna
Nipuna

Reputation: 7006

Regex to match method content

To develop a Java Code Parser, I want to extract method contents of a java source file as strings. But the problem is I can't match the content from Regex by getting value between { and } because some methods has { 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("(?<={).*?(?=})");

It only captures try{ //something

How can i ignore occurences of { and } inside method and get value inside method like

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

   }
 return args;

Upvotes: 5

Views: 2274

Answers (2)

Olivier
Olivier

Reputation: 5688

Try the following regex on C#-like text. It will capture every method body, taking nested {} into account. For explanations : 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);

[edit] Sorry, I forgot the "RegexOptions.IgnorePatternWhitespace"

This sample is writing to console :

{d{e}f}

{i}

Upvotes: 2

dtb
dtb

Reputation: 217283

If your code doesn't have to work with arbitrary input, you can take advantage of coding conventions in your input file to find the methods. For example, in most coding guidelines, methods always start on a new line, and the closing brace has the same indentation as the opening brace.

If your code has to work with arbitrary input, regexes are the wrong tool. You need a Java parser.

Upvotes: 0

Related Questions