import Foundation
let pattern = #"(\(\)\n[[:space:]]{1,}\{)|(\(\)[[:space:]]{1,}\{)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
// feedback thread
import java.util.*;
class Main
{
static Scanner scan = new Scanner(System.in);
static String deadEnd = "dead end--morphing backwards!";
static String gettingCloser = "You'r getting closer";
// call this method after each decision has been made
public static void clearScreen() {
Scanner scan = new Scanner(System.in);
System.out.println("Press enter to continue.");
String x = scan.nextLine();
System.out.print("\033[H\033[2J");
System.out.flush();
}
public static void decisionZero()
{
System.out.println("Decision Point 0");
System.out.println("Choose a direction Mr. Mouse");
String direction = scan.nextLine();
switch (direction.toLowerCase()) {
case "n":
System.out.println("You chose north.");
System.out.println(deadEnd);
decisionZero();
break;
case "s":
System.out.println("You chose south.");
System.out.println(gettingCloser);
decisionOne();
break;
case "e":
System.out.println("You chose east.");
System.out.println(deadEnd);
decisionZero();
break;
case "w":
System.out.println("You chose west.");
System.out.println(deadEnd);
decisionZero();
break;
}
}
public static void decisionOne()
{
System.out.println("Decision Point 1");
System.out.println("Choose a direction Mr. Mouse");
String direction = scan.nextLine();
switch (direction.toLowerCase()) {
case "n":
System.out.println("You chose north.");
System.out.println(deadEnd);
decisionZero();
break;
case "s":
System.out.println("You chose south.");
System.out.println(deadEnd);
decisionZero();
break;
case "e":
System.out.println("You chose east.");
System.out.println(deadEnd);
decisionZero();
break;
case "w":
System.out.println("You chose west.");
System.out.println(gettingCloser);
decisionTwo();
break;
}
}
public static void decisionTwo()
{
System.out.println("Decision Point 2");
System.out.println("Choose a direction Mr. Mouse");
String direction = scan.nextLine();
int rndDecion = (int)(Math.random()*2);
switch (direction.toLowerCase()) {
case "n":
System.out.println("You chose north.");
System.out.println(gettingCloser);
decisionThree();
break;
case "s":
System.out.println("You chose south.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
}
break;
case "e":
System.out.println("You chose east.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
}
break;
case "w":
System.out.println("You chose west.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
}
break;
}
}
public static void decisionThree()
{
System.out.println("Decision Point 3");
System.out.println("Choose a direction Mr. Mouse");
String direction = scan.nextLine();
int rndDecion = (int)(Math.random()*3);
switch (direction.toLowerCase()) {
case "n":
System.out.println("You chose north.");
System.out.println(gettingCloser);
decisionFour();
break;
case "s":
System.out.println("You chose south.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
}
break;
case "e":
System.out.println("You chose east.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
}
break;
case "w":
System.out.println("You chose west.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
}
break;
}
}
public static void decisionFour()
{
System.out.println("Decision Point 4");
System.out.println("Choose a direction Mr. Mouse");
String direction = scan.nextLine();
int rndDecion = (int)(Math.random()*4);
switch (direction.toLowerCase()) {
case "n":
System.out.println("You chose north.");
System.out.println(gettingCloser);
decisionFive();
break;
case "s":
System.out.println("You chose south.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
}
break;
case "e":
System.out.println("You chose east.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
}
break;
case "w":
System.out.println("You chose west.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
}
break;
}
}
public static void decisionFive()
{
System.out.println("Decision Point 5");
System.out.println("Choose a direction Mr. Mouse");
String direction = scan.nextLine();
int rndDecion = (int)(Math.random()*5);
switch (direction.toLowerCase()) {
case "n":
System.out.println("You chose north.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
case 4:
decisionFour();
}
break;
case "s":
System.out.println("You chose south.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
case 4:
decisionFour();
}
break;
case "e":
System.out.println("You chose east.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
case 4:
decisionFour();
}
break;
case "w":
System.out.println("You chose west.");
System.out.println(gettingCloser);
decisionSix();
break;
}
}
public static void decisionSix()
{
System.out.println("Decision Point 6");
System.out.println("Choose a direction Mr. Mouse");
String direction = scan.nextLine();
int rndDecion = (int)(Math.random()*5);
switch (direction.toLowerCase()) {
case "n":
System.out.println("You chose north.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
case 4:
decisionFour();
}
break;
case "s":
System.out.println("You chose south.");
System.out.println(deadEnd);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
case 4:
decisionFour();
}
break;
case "e":
System.out.println("You chose east.");
System.out.println("Victory cheese!");
break;
case "w":
System.out.println("You chose west.");
System.out.println(gettingCloser);
switch (rndDecion) {
case 0:
decisionZero();
case 1:
decisionOne();
case 2:
decisionTwo();
case 3:
decisionThree();
case 4:
decisionFour();
}
break;
}
}
// the main method is COMPLETE. Add/change nothing here.
public static void main(String[] args)
{
decisionZero(); //only line in main method.
}
}
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"()\n {\n clearScreen();"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression