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;
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
[edit]
Sorry, I forgot the “RegexOptions.IgnorePatternWhitespace”
This sample is writing to console :
{d{e}f}
{i}