Take an example.
public static FieldsConfig getFieldsConfig(){
if(xxx) {
sssss;
}
return;
}
I write a regex, \"\\\\
Thank all of you. After some consideration, I work out a reliable way to some degree in my situation. Now share it.
String regex ="\\s*public\s+static\s+[\w\.\<\>,\s]+\s+getFieldsConfig\\(.*?\\)\\s*\\{.*?\\}(?=\\s*(public|private|protected|static))";
String regex2 = "\\s*public\s+static\s+[\w\.\<\>,\s]+\s+getFieldsConfig\\(.*?\\)\\s*\\{.*?\\}(?=(\\s*}\\s*$))";
regex = "(" + regex +")|("+ regex2 + "){1}?";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL)
It can match my method body well.
PS Yes, the regex maybe not the suitable way to parse a method very strictly. Generally speaking, regex is less effort than programming and work right in specific situation. Adjust it and Sure it works for you.