import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<indent>\\s*)(?<mod1>\\w+)\\s(?<mod2>\\w+)?\\s*(?<mod3>\\w+)?\\s*(?<return>\\b\\w+)\\s(?<name>\\w+)\\((?<arg>.*?)\\)\\s*\\{(?<body>.+?)^\\k<indent>\\}";
final String string = "public static FieldsConfig getFieldsConfig(){\n"
+ " if(xxx) {\n"
+ " sssss;\n"
+ " }\n"
+ " return;\n"
+ "}\n\n"
+ " public String getAsSeparateGroup(String groupName) {\n"
+ " String begin = pattern.substring(0,getStart());\n"
+ " String mid = pattern.substring(getStart(),getEnd());\n"
+ " String finish = pattern.substring(getEnd());\n"
+ " return begin + \"(?<\" + groupName + \">\" + mid + \")\" + finish;\n"
+ " }\n\n"
+ " @Override\n"
+ " public static final String toString() {\n"
+ " return asString;\n"
+ " }\n\n"
+ " public String directMatch(String match) {\n"
+ " Matcher matcher = Pattern.compile(getAsSeparateGroup(\"test\")).matcher(match);\n"
+ " matcher.find();\n"
+ " String result;\n"
+ " try {\n"
+ " result = matcher.group(\"test\");\n"
+ " }catch (Exception ex) {\n"
+ " result = \"empty\";\n"
+ " }\n"
+ " return getPattern() + \":\" + result;\n"
+ " }\n"
+ "}";
final String subst = "";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html