Take an example.
public static FieldsConfig getFieldsConfig(){
if(xxx) {
sssss;
}
return;
}
I write a regex, \"\\\\
You need to enable DOTALL mode. Then dot will match newLine chars. Just include (?s)
in the beginning of your regex.
String s = " public static FieldsConfig getFieldsConfig(){\n"
+ " if(xxx) {\n"
+ " sssss;\n"
+ " }\n"
+ " return;\n"
+"}";
Matcher m = Pattern.compile("(?s)\\s*public\\s+static\\s+\\w+?\\sgetFieldsConfig\\(\\s*\\).*").matcher(s);
m.find();
System.out.println(m.group());
Outpup is all method body as you wanted. Without (?s)
it matches only the first line. But you cannot parse java code with regex. Others already said that. This regex will match everything from beginning of method signature to the end of file. How do you match it only until the end of method body is reached? Method can contain many {....}
as well as many return;
. Regex is not a magic stick.