import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\/\\/.*+|\\/[*]{1,2}([^*]*[^*\\/]|\\R++|[*]+[^*\\/])*[*]\\/)";
final String string = "// This file is part of www.nand2tetris.org\n"
+ "// and the book \"The Elements of Computing Systems\"\n"
+ "// by Nisan and Schocken, MIT Press.\n"
+ "// File name: projects/10/ArrayTest/Main.jack\n\n"
+ "// (identical to projects/09/Average/Main.jack)\n\n"
+ "/** Computes the average of a sequence of integers. */\n"
+ "class Main {\n"
+ " function void main() {\n"
+ " var Array a;\n"
+ " var int length;\n"
+ " var int i, sum;\n"
+ " \n"
+ " let length = Keyboard.readInt(\"HOW MANY NUMBERS? \");\n"
+ " let a = Array.new(length);\n"
+ " let i = 0;\n"
+ " \n"
+ " while (i < length) {\n"
+ " let a[i] = Keyboard.readInt(\"ENTER THE NEXT NUMBER: \");\n"
+ " let i = i + 1;\n"
+ " }\n"
+ " \n"
+ " let i = 0;\n"
+ " let sum = 0;\n"
+ " \n"
+ " while (i < length) {\n"
+ " let sum = sum + a[i];\n"
+ " let i = i + 1;\n"
+ " }\n"
+ " \n"
+ " do Output.printString(\"THE AVERAGE IS: \");\n"
+ " do Output.printInt(sum / length);\n"
+ " do Output.println();\n"
+ " \n"
+ " return;\n"
+ " }\n"
+ "}\n"
+ "// This file is part of www.nand2tetris.org\n"
+ "// and the book \"The Elements of Computing Systems\"\n"
+ "// by Nisan and Schocken, MIT Press.\n"
+ "// File name: projects/10/ExpressionLessSquare/Main.jack\n\n"
+ "/** Expressionless version of projects/10/Square/Main.jack. */\n\n"
+ "class Main {\n"
+ " static boolean test; // Added for testing -- there is no static keyword\n"
+ " // in the Square files.\n\n"
+ " function void main() {\n"
+ " var SquareGame game;\n"
+ " let game = game;\n"
+ " do game.run();\n"
+ " do game.dispose();\n"
+ " return;\n"
+ " }\n\n"
+ " function void more() { // Added to test Jack syntax that is not used in\n"
+ " var boolean b; // the Square files.\n"
+ " if (b) {\n"
+ " }\n"
+ " else { // There is no else keyword in the Square files.\n"
+ " }\n"
+ " return;\n"
+ " }\n"
+ "}\n"
+ "// This file is part of www.nand2tetris.org\n"
+ "// and the book \"The Elements of Computing Systems\"\n"
+ "// by Nisan and Schocken, MIT Press.\n"
+ "/// File name: projects/10/ExpressionLessSquare/Square.jack\n\n"
+ "/** Expressionless version of projects/10/Square/Square.jack. */\n\n"
+ "class Square {\n\n"
+ " field int x, y; \n"
+ " field int size; \n\n"
+ " constructor Square new(int Ax, int Ay, int Asize) {\n"
+ " let x = Ax;\n"
+ " let y = Ay;\n"
+ " let size = Asize;\n"
+ " do draw();\n"
+ " return x;\n"
+ " }\n\n"
+ " method void dispose() {\n"
+ " do Memory.deAlloc(this);\n"
+ " return;\n"
+ " }\n\n"
+ " method void draw() {\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " return;\n"
+ " }\n\n"
+ " method void erase() {\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " return;\n"
+ " }\n\n"
+ " method void incSize() {\n"
+ " if (x) {\n"
+ " do erase();\n"
+ " let size = size;\n"
+ " do draw();\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " method void decSize() {\n"
+ " if (size) {\n"
+ " do erase();\n"
+ " let size = size;\n"
+ " do draw();\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " method void moveUp() {\n"
+ " if (y) {\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " let y = y;\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " method void moveDown() {\n"
+ " if (y) {\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " let y = y;\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " method void moveLeft() {\n"
+ " if (x) {\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " let x = x;\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " method void moveRight() {\n"
+ " if (x) {\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " let x = x;\n"
+ " do Screen.setColor(x);\n"
+ " do Screen.drawRectangle(x, y, x, y);\n"
+ " }\n"
+ " return;\n"
+ " }\n"
+ "} \n"
+ "// This file is part of www.nand2tetris.org\n"
+ "// and the book \"The Elements of Computing Systems\"\n"
+ "// by Nisan and Schocken, MIT Press.\n"
+ "// File name: projects/10/ExpressionLessSquare/SquareGame.jack\n\n"
+ "/** Expressionless version of projects/10/Square/SquareGame.jack. */\n\n"
+ "class SquareGame {\n"
+ " field Square square; \n"
+ " field int direction; \n\n"
+ " constructor SquareGame new() {\n"
+ " let square = square;\n"
+ " let direction = direction;\n"
+ " return square;\n"
+ " }\n\n"
+ " method void dispose() {\n"
+ " do square.dispose();\n"
+ " do Memory.deAlloc(square);\n"
+ " return;\n"
+ " }\n\n"
+ " method void moveSquare() {\n"
+ " if (direction) { do square.moveUp(); }\n"
+ " if (direction) { do square.moveDown(); }\n"
+ " if (direction) { do square.moveLeft(); }\n"
+ " if (direction) { do square.moveRight(); }\n"
+ " do Sys.wait(direction);\n"
+ " return;\n"
+ " }\n\n"
+ " method void run() {\n"
+ " var char key;\n"
+ " var boolean exit;\n"
+ " \n"
+ " let exit = key;\n"
+ " while (exit) {\n"
+ " while (key) {\n"
+ " let key = key;\n"
+ " do moveSquare();\n"
+ " }\n\n"
+ " if (key) { let exit = exit; }\n"
+ " if (key) { do square.decSize(); }\n"
+ " if (key) { do square.incSize(); }\n"
+ " if (key) { let direction = exit; }\n"
+ " if (key) { let direction = key; }\n"
+ " if (key) { let direction = square; }\n"
+ " if (key) { let direction = direction; }\n\n"
+ " while (key) {\n"
+ " let key = key;\n"
+ " do moveSquare();\n"
+ " }\n"
+ " }\n"
+ " return;\n"
+ " }\n"
+ "}\n\n"
+ "// This file is part of www.nand2tetris.org\n"
+ "// and the book \"The Elements of Computing Systems\"\n"
+ "// by Nisan and Schocken, MIT Press.\n"
+ "// File name: projects/10/Square/Main.jack\n\n"
+ "// (derived from projects/09/Square/Main.jack, with testing additions)\n\n"
+ "/** Initializes a new Square Dance game and starts running it. */\n"
+ "class Main {\n"
+ " static boolean test; // Added for testing -- there is no static keyword\n"
+ " // in the Square files.\n"
+ " function void main() {\n"
+ " var SquareGame game;\n"
+ " let game = SquareGame.new();\n"
+ " do game.run();\n"
+ " do game.dispose();\n"
+ " return;\n"
+ " }\n\n"
+ " function void more() { // Added to test Jack syntax that is not used in\n"
+ " var int i, j; // the Square files.\n"
+ " var String s;\n"
+ " var Array a;\n"
+ " if (false) {\n"
+ " let s = \"string constant\";\n"
+ " let s = null;\n"
+ " let a[1] = a[2];\n"
+ " }\n"
+ " else { // There is no else keyword in the Square files.\n"
+ " let i = i * (-j);\n"
+ " let j = j / (-2); // note: unary negate constant 2\n"
+ " let i = i | j;\n"
+ " }\n"
+ " return;\n"
+ " }\n"
+ "}\n"
+ "// This file is part of www.nand2tetris.org\n"
+ "// and the book \"The Elements of Computing Systems\"\n"
+ "// by Nisan and Schocken, MIT Press.\n"
+ "// File name: projects/10/Square/Square.jack\n\n"
+ "// (same as projects/09/Square/Square.jack)\n\n"
+ "/** Implements a graphical square. */\n"
+ "class Square {\n\n"
+ " field int x, y; // screen location of the square's top-left corner\n"
+ " field int size; // length of this square, in pixels\n\n"
+ " /** Constructs a new square with a given location and size. */\n"
+ " constructor Square new(int Ax, int Ay, int Asize) {\n"
+ " let x = Ax;\n"
+ " let y = Ay;\n"
+ " let size = Asize;\n"
+ " do draw();\n"
+ " return this;\n"
+ " }\n\n"
+ " /** Disposes this square. */\n"
+ " method void dispose() {\n"
+ " do Memory.deAlloc(this);\n"
+ " return;\n"
+ " }\n\n"
+ " /** Draws the square on the screen. */\n"
+ " method void draw() {\n"
+ " do Screen.setColor(true);\n"
+ " do Screen.drawRectangle(x, y, x + size, y + size);\n"
+ " return;\n"
+ " }\n\n"
+ " /** Erases the square from the screen. */\n"
+ " method void erase() {\n"
+ " do Screen.setColor(false);\n"
+ " do Screen.drawRectangle(x, y, x + size, y + size);\n"
+ " return;\n"
+ " }\n\n"
+ " /** Increments the square size by 2 pixels. */\n"
+ " method void incSize() {\n"
+ " if (((y + size) < 254) & ((x + size) < 510)) {\n"
+ " do erase();\n"
+ " let size = size + 2;\n"
+ " do draw();\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " /** Decrements the square size by 2 pixels. */\n"
+ " method void decSize() {\n"
+ " if (size > 2) {\n"
+ " do erase();\n"
+ " let size = size - 2;\n"
+ " do draw();\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " /** Moves the square up by 2 pixels. */\n"
+ " method void moveUp() {\n"
+ " if (y > 1) {\n"
+ " do Screen.setColor(false);\n"
+ " do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size);\n"
+ " let y = y - 2;\n"
+ " do Screen.setColor(true);\n"
+ " do Screen.drawRectangle(x, y, x + size, y + 1);\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " /** Moves the square down by 2 pixels. */\n"
+ " method void moveDown() {\n"
+ " if ((y + size) < 254) {\n"
+ " do Screen.setColor(false);\n"
+ " do Screen.drawRectangle(x, y, x + size, y + 1);\n"
+ " let y = y + 2;\n"
+ " do Screen.setColor(true);\n"
+ " do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size);\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " /** Moves the square left by 2 pixels. */\n"
+ " method void moveLeft() {\n"
+ " if (x > 1) {\n"
+ " do Screen.setColor(false);\n"
+ " do Screen.drawRectangle((x + size) - 1, y, x + size, y + size);\n"
+ " let x = x - 2;\n"
+ " do Screen.setColor(true);\n"
+ " do Screen.drawRectangle(x, y, x + 1, y + size);\n"
+ " }\n"
+ " return;\n"
+ " }\n\n"
+ " /** Moves the square right by 2 pixels. */\n"
+ " method void moveRight() {\n"
+ " if ((x + size) < 510) {\n"
+ " do Screen.setColor(false);\n"
+ " do Screen.drawRectangle(x, y, x + 1, y + size);\n"
+ " let x = x + 2;\n"
+ " do Screen.setColor(true);\n"
+ " do Screen.drawRectangle((x + size) - 1, y, x + size, y + size);\n"
+ " }\n"
+ " return;\n"
+ " }\n"
+ "}\n"
+ "// This file is part of www.nand2tetris.org\n"
+ "// and the book \"The Elements of Computing Systems\"\n"
+ "// by Nisan and Schocken, MIT Press.\n"
+ "// File name: projects/10/Square/SquareGame.jack\n\n"
+ "// (same as projects/09/Square/SquareGame.jack)\n\n"
+ "/**\n"
+ " * Implements the Square Dance game.\n"
+ " * This simple game allows the user to move a black square around\n"
+ " * the screen, and change the square's size during the movement.\n"
+ " * When the game starts, a square of 30 by 30 pixels is shown at the\n"
+ " * top-left corner of the screen. The user controls the square as follows.\n"
+ " * The 4 arrow keys are used to move the square up, down, left, and right.\n"
+ " * The 'z' and 'x' keys are used, respectively, to decrement and increment\n"
+ " * the square's size. The 'q' key is used to quit the game.\n"
+ " */\n\n"
+ "class SquareGame {\n"
+ " field Square square; // the square of this game\n"
+ " field int direction; // the square's current direction: \n"
+ " // 0=none, 1=up, 2=down, 3=left, 4=right\n\n"
+ " /** Constructs a new Square Game. */\n"
+ " constructor SquareGame new() {\n"
+ " // Creates a 30 by 30 pixels square and positions it at the top-left\n"
+ " // of the screen.\n"
+ " let square = Square.new(0, 0, 30);\n"
+ " let direction = 0; // initial state is no movement\n"
+ " return this;\n"
+ " }\n\n"
+ " /** Disposes this game. */\n"
+ " method void dispose() {\n"
+ " do square.dispose();\n"
+ " do Memory.deAlloc(this);\n"
+ " return;\n"
+ " }\n\n"
+ " /** Moves the square in the current direction. */\n"
+ " method void moveSquare() {\n"
+ " if (direction = 1) { do square.moveUp(); }\n"
+ " if (direction = 2) { do square.moveDown(); }\n"
+ " if (direction = 3) { do square.moveLeft(); }\n"
+ " if (direction = 4) { do square.moveRight(); }\n"
+ " do Sys.wait(5); // delays the next movement\n"
+ " return;\n"
+ " }\n\n"
+ " /** Runs the game: handles the user's inputs and moves the square accordingly */\n"
+ " method void run() {\n"
+ " var char key; // the key currently pressed by the user\n"
+ " var boolean exit;\n"
+ " let exit = false;\n"
+ " \n"
+ " while (~exit) {\n"
+ " // waits for a key to be pressed\n"
+ " while (key = 0) {\n"
+ " let key = Keyboard.keyPressed();\n"
+ " do moveSquare();\n"
+ " }\n"
+ " if (key = 81) { let exit = true; } // q key\n"
+ " if (key = 90) { do square.decSize(); } // z key\n"
+ " if (key = 88) { do square.incSize(); } // x key\n"
+ " if (key = 131) { let direction = 1; } // up arrow\n"
+ " if (key = 133) { let direction = 2; } // down arrow\n"
+ " if (key = 130) { let direction = 3; } // left arrow\n"
+ " if (key = 132) { let direction = 4; } // right arrow\n\n"
+ " // waits for the key to be released\n"
+ " while (~(key = 0)) {\n"
+ " let key = Keyboard.keyPressed();\n"
+ " do moveSquare();\n"
+ " }\n"
+ " } // while\n"
+ " return;\n"
+ " }\n"
+ "}\n\n\n\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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