import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\(\\)\\n[[:space:]]{1,}\\{)|(\\(\\)[[:space:]]{1,}\\{)";
final String string = "// feedback thread\n\n"
+ "import java.util.*;\n\n"
+ "class Main\n"
+ "{\n"
+ " static Scanner scan = new Scanner(System.in);\n"
+ " static String deadEnd = \"dead end--morphing backwards!\";\n"
+ " static String gettingCloser = \"You'r getting closer\";\n"
+ " \n"
+ " // call this method after each decision has been made\n"
+ " public static void clearScreen() {\n"
+ " Scanner scan = new Scanner(System.in);\n"
+ " System.out.println(\"Press enter to continue.\");\n"
+ " String x = scan.nextLine(); \n"
+ " System.out.print(\"\\033[H\\033[2J\"); \n"
+ " System.out.flush(); \n"
+ " } \n\n\n\n"
+ " public static void decisionZero()\n"
+ " {\n"
+ " System.out.println(\"Decision Point 0\");\n"
+ " System.out.println(\"Choose a direction Mr. Mouse\");\n"
+ " String direction = scan.nextLine();\n"
+ " switch (direction.toLowerCase()) {\n"
+ " case \"n\":\n"
+ " System.out.println(\"You chose north.\");\n"
+ " System.out.println(deadEnd);\n"
+ " decisionZero();\n"
+ " break;\n\n"
+ " case \"s\":\n"
+ " System.out.println(\"You chose south.\");\n"
+ " System.out.println(gettingCloser);\n"
+ " decisionOne();\n"
+ " break;\n"
+ " \n"
+ " case \"e\":\n"
+ " System.out.println(\"You chose east.\");\n"
+ " System.out.println(deadEnd);\n"
+ " decisionZero();\n"
+ " break;\n\n"
+ " case \"w\":\n"
+ " System.out.println(\"You chose west.\");\n"
+ " System.out.println(deadEnd);\n"
+ " decisionZero();\n"
+ " break;\n"
+ " \n"
+ " }\n"
+ " }\n"
+ " \n"
+ " public static void decisionOne()\n"
+ " {\n"
+ " System.out.println(\"Decision Point 1\");\n"
+ " System.out.println(\"Choose a direction Mr. Mouse\");\n"
+ " String direction = scan.nextLine();\n"
+ " switch (direction.toLowerCase()) {\n"
+ " case \"n\":\n"
+ " System.out.println(\"You chose north.\");\n"
+ " System.out.println(deadEnd);\n"
+ " decisionZero();\n"
+ " break;\n\n"
+ " case \"s\":\n"
+ " System.out.println(\"You chose south.\");\n"
+ " System.out.println(deadEnd);\n"
+ " decisionZero();\n"
+ " break;\n\n"
+ " case \"e\":\n"
+ " System.out.println(\"You chose east.\");\n"
+ " System.out.println(deadEnd);\n"
+ " decisionZero();\n"
+ " break;\n\n"
+ " case \"w\":\n"
+ " System.out.println(\"You chose west.\");\n"
+ " System.out.println(gettingCloser);\n"
+ " decisionTwo();\n"
+ " break;\n\n"
+ " }\n"
+ " }\n"
+ " \n"
+ " public static void decisionTwo()\n"
+ " {\n"
+ " System.out.println(\"Decision Point 2\");\n"
+ " System.out.println(\"Choose a direction Mr. Mouse\");\n"
+ " String direction = scan.nextLine();\n"
+ " int rndDecion = (int)(Math.random()*2);\n"
+ " switch (direction.toLowerCase()) {\n"
+ " case \"n\":\n"
+ " System.out.println(\"You chose north.\");\n"
+ " System.out.println(gettingCloser);\n"
+ " decisionThree();\n"
+ " break;\n\n"
+ " case \"s\":\n"
+ " System.out.println(\"You chose south.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"e\":\n"
+ " System.out.println(\"You chose east.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"w\":\n"
+ " System.out.println(\"You chose west.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " }\n"
+ " break;\n\n"
+ " }\n"
+ " }\n"
+ " \n"
+ " public static void decisionThree()\n"
+ " {\n"
+ " System.out.println(\"Decision Point 3\");\n"
+ " System.out.println(\"Choose a direction Mr. Mouse\");\n"
+ " String direction = scan.nextLine();\n"
+ " int rndDecion = (int)(Math.random()*3);\n"
+ " switch (direction.toLowerCase()) {\n"
+ " case \"n\":\n"
+ " System.out.println(\"You chose north.\");\n"
+ " System.out.println(gettingCloser);\n"
+ " decisionFour();\n"
+ " break;\n\n"
+ " case \"s\":\n"
+ " System.out.println(\"You chose south.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"e\":\n"
+ " System.out.println(\"You chose east.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"w\":\n"
+ " System.out.println(\"You chose west.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " }\n"
+ " break;\n\n"
+ " }\n"
+ " }\n"
+ " \n"
+ " public static void decisionFour()\n"
+ " {\n"
+ " System.out.println(\"Decision Point 4\");\n"
+ " System.out.println(\"Choose a direction Mr. Mouse\");\n"
+ " String direction = scan.nextLine();\n"
+ " int rndDecion = (int)(Math.random()*4);\n"
+ " switch (direction.toLowerCase()) {\n"
+ " case \"n\":\n"
+ " System.out.println(\"You chose north.\");\n"
+ " System.out.println(gettingCloser);\n"
+ " decisionFive();\n"
+ " break;\n\n"
+ " case \"s\":\n"
+ " System.out.println(\"You chose south.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"e\":\n"
+ " System.out.println(\"You chose east.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"w\":\n"
+ " System.out.println(\"You chose west.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " }\n"
+ " break;\n\n"
+ " }\n"
+ " }\n\n"
+ " public static void decisionFive()\n"
+ " {\n"
+ " System.out.println(\"Decision Point 5\");\n"
+ " System.out.println(\"Choose a direction Mr. Mouse\");\n"
+ " String direction = scan.nextLine();\n"
+ " int rndDecion = (int)(Math.random()*5);\n"
+ " switch (direction.toLowerCase()) {\n"
+ " case \"n\":\n"
+ " System.out.println(\"You chose north.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " case 4:\n"
+ " decisionFour();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"s\":\n"
+ " System.out.println(\"You chose south.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " case 4:\n"
+ " decisionFour();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"e\":\n"
+ " System.out.println(\"You chose east.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " case 4:\n"
+ " decisionFour();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"w\":\n"
+ " System.out.println(\"You chose west.\");\n"
+ " System.out.println(gettingCloser);\n"
+ " decisionSix();\n"
+ " break;\n\n"
+ " }\n"
+ " }\n\n"
+ " public static void decisionSix()\n"
+ " {\n"
+ " System.out.println(\"Decision Point 6\");\n"
+ " System.out.println(\"Choose a direction Mr. Mouse\");\n"
+ " String direction = scan.nextLine();\n"
+ " int rndDecion = (int)(Math.random()*5);\n"
+ " switch (direction.toLowerCase()) {\n"
+ " case \"n\":\n"
+ " System.out.println(\"You chose north.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " case 4:\n"
+ " decisionFour();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"s\":\n"
+ " System.out.println(\"You chose south.\");\n"
+ " System.out.println(deadEnd);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " case 4:\n"
+ " decisionFour();\n"
+ " }\n"
+ " break;\n\n"
+ " case \"e\":\n"
+ " System.out.println(\"You chose east.\");\n"
+ " System.out.println(\"Victory cheese!\");\n"
+ " break;\n\n"
+ " case \"w\":\n"
+ " System.out.println(\"You chose west.\");\n"
+ " System.out.println(gettingCloser);\n"
+ " switch (rndDecion) {\n"
+ " case 0:\n"
+ " decisionZero();\n"
+ " case 1:\n"
+ " decisionOne();\n"
+ " case 2:\n"
+ " decisionTwo();\n"
+ " case 3:\n"
+ " decisionThree();\n"
+ " case 4:\n"
+ " decisionFour();\n"
+ " }\n"
+ " break;\n\n"
+ " }\n"
+ " }\n\n\n"
+ " // the main method is COMPLETE. Add/change nothing here.\n"
+ " public static void main(String[] args) \n"
+ " {\n"
+ " decisionZero(); //only line in main method.\n"
+ " }\n\n\n\n"
+ "}";
final String subst = "()\\n {\\n clearScreen();";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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