# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(((import java\.)[a-zA-Z\.\*]*;)|(public static void main(.*))|(class\s[a-zA-Z_$]+[\s]*\{)|(System\.out\.print))"
test_str = ("/*\n"
" Calculate Circle Area using Java Example\n"
" This Calculate Circle Area using Java Example shows how to calculate\n"
" area of circle using it's radius.\n"
"*/\n"
" \n"
"import java.io.BufferedReader;\n"
"import java.io.IOException;\n"
"import java.io.InputStreamReader;\n"
" \n"
"public class CalculateCircleAreaExample {\n"
" \n"
" public static void main(String[] args) {\n"
" \n"
" int radius = 0;\n"
" System.out.println(\"Please enter radius of a circle\");\n"
" \n"
" try\n"
" {\n"
" //get the radius from console\n"
" BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n"
" radius = Integer.parseInt(br.readLine());\n"
" }\n"
" //if invalid value was entered\n"
" catch(NumberFormatException ne)\n"
" {\n"
" System.out.println(\"Invalid radius value\" + ne);\n"
" System.exit(0);\n"
" }\n"
" catch(IOException ioe)\n"
" {\n"
" System.out.println(\"IO Error :\" + ioe);\n"
" System.exit(0);\n"
" }\n"
" \n"
" /*\n"
" * Area of a circle is\n"
" * pi * r * r\n"
" * where r is a radius of a circle.\n"
" */\n"
" \n"
" //NOTE : use Math.PI constant to get value of pi\n"
" double area = Math.PI * radius * radius;\n"
" \n"
" System.out.println(\"Area of a circle is \" + area);\n"
" }\n"
"}\n"
" \n"
"/*\n"
"Output of Calculate Circle Area using Java Example would be\n"
"Please enter radius of a circle\n"
"19\n"
"Area of a circle is 1134.1149479459152\n"
"*/\n\n"
"@@@@\n\n"
"/*\n"
" Calculate Rectangle Area using Java Example\n"
" This Calculate Rectangle Area using Java Example shows how to calculate\n"
" area of Rectangle using it's length and width.\n"
"*/\n"
" \n"
"import java.io.BufferedReader;\n"
"import java.io.IOException;\n"
"import java.io.InputStreamReader;\n"
" \n"
"public class CalculateRectArea {\n"
" \n"
" public static void main(String[] args) {\n"
" \n"
" int width = 0;\n"
" int length = 0;\n"
" \n"
" try\n"
" {\n"
" //read the length from console\n"
" BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n"
" \n"
" System.out.println(\"Please enter length of a rectangle\");\n"
" length = Integer.parseInt(br.readLine());\n"
" \n"
" //read the width from console\n"
" System.out.println(\"Please enter width of a rectangle\");\n"
" width = Integer.parseInt(br.readLine());\n"
" \n"
" \n"
" }\n"
" //if invalid value was entered\n"
" catch(NumberFormatException ne)\n"
" {\n"
" System.out.println(\"Invalid value\" + ne);\n"
" System.exit(0);\n"
" }\n"
" catch(IOException ioe)\n"
" {\n"
" System.out.println(\"IO Error :\" + ioe);\n"
" System.exit(0);\n"
" }\n"
" \n"
" /*\n"
" * Area of a rectangle is\n"
" * length * width\n"
" */\n"
" \n"
" int area = length * width;\n"
" \n"
" System.out.println(\"Area of a rectangle is \" + area);\n"
" }\n"
" \n"
"}\n"
" \n"
"/*\n"
"Output of Calculate Rectangle Area using Java Example would be\n"
"Please enter length of a rectangle\n"
"10\n"
"Please enter width of a rectangle\n"
"15\n"
"Area of a rectangle is 150\n"
"*/\n\n"
"@@@@\n\n"
"/*\n"
" Java Factorial Using Recursion Example\n"
" This Java example shows how to generate factorial of a given number\n"
" using recursive function.\n"
"*/\n"
" \n"
"import java.io.BufferedReader;\n"
"import java.io.IOException;\n"
"import java.io.InputStreamReader;\n"
" \n"
"public class JavaFactorialUsingRecursion {\n"
" \n"
" public static void main(String args[]) throws NumberFormatException, IOException{\n"
" \n"
" System.out.println(\"Enter the number: \");\n"
" \n"
" //get input from the user\n"
" BufferedReader br=new BufferedReader(new InputStreamReader(System.in));\n"
" int a = Integer.parseInt(br.readLine());\n"
" \n"
" //call the recursive function to generate factorial\n"
" int result= fact(a);\n"
" \n"
" \n"
" System.out.println(\"Factorial of the number is: \" + result);\n"
" }\n"
" \n"
" static int fact(int b)\n"
" {\n"
" if(b <= 1)\n"
" //if the number is 1 then return 1\n"
" return 1;\n"
" else\n"
" //else call the same function with the value - 1\n"
" return b * fact(b-1);\n"
" }\n"
"}\n"
" \n"
"/*\n"
"Output of this Java example would be\n"
" \n"
"Enter the number:\n"
"5\n"
"Factorial of the number is: 120\n"
"*/\n\n"
"@@@@\n\n"
"/*\n"
" Swap Numbers Without Using Third Variable Java Example\n"
" This Swap Numbers Java Example shows how to\n"
" swap value of two numbers without using third variable using java.\n"
"*/\n"
" \n"
"public class SwapElementsWithoutThirdVariableExample {\n"
" \n"
" public static void main(String[] args) {\n"
" \n"
" int num1 = 10;\n"
" int num2 = 20;\n"
" \n"
" System.out.println(\"Before Swapping\");\n"
" System.out.println(\"Value of num1 is :\" + num1);\n"
" System.out.println(\"Value of num2 is :\" +num2);\n"
" \n"
" //add both the numbers and assign it to first\n"
" num1 = num1 + num2;\n"
" num2 = num1 - num2;\n"
" num1 = num1 - num2;\n"
" \n"
" System.out.println(\"Before Swapping\");\n"
" System.out.println(\"Value of num1 is :\" + num1);\n"
" System.out.println(\"Value of num2 is :\" +num2);\n"
" }\n"
" \n"
" \n"
"}\n"
" \n"
"/*\n"
"Output of Swap Numbers Without Using Third Variable example would be\n"
"Before Swapping\n"
"Value of num1 is :10\n"
"Value of num2 is :20\n"
"Before Swapping\n"
"Value of num1 is :20\n"
"Value of num2 is :10\n"
"*/\n\n"
"@@@@\n\n"
"// OddEven.java\n"
"import javax.swing.JOptionPane;\n"
" \n"
"public class OddEven {\n"
" /**\n"
" * \"input\" is the number that the user gives to the computer\n"
" */\n"
" private int input; // a whole number(\"int\" means integer)\n"
" \n"
" /**\n"
" * This is the constructor method. It gets called when an object of the OddEven type\n"
" * is being created.\n"
" */\n"
" public OddEven() {\n"
" /*\n"
" * In most Java programs constructors can initialize objects with default values, or create\n"
" * other objects that this object might use to perform its functions. In some Java programs, the\n"
" * constructor may simply be an empty function if nothing needs to be initialized prior to the\n"
" * functioning of the object. In this program's case, an empty constructor would suffice.\n"
" * A constructor must exist; however, if the user doesn't put one in then the compiler\n"
" * will create an empty one.\n"
" */\n"
" }\n"
" \n"
" /**\n"
" * This is the main method. It gets called when this class is run through a Java interpreter.\n"
" * @param args command line arguments (unused)\n"
" */\n"
" public static void main(final String[] args) {\n"
" /*\n"
" * This line of code creates a new instance of this class called \"number\" (also known as an\n"
" * Object) and initializes it by calling the constructor. The next line of code calls\n"
" * the \"showDialog()\" method, which brings up a prompt to ask you for a number\n"
" */\n"
" OddEven number = new OddEven();\n"
" number.showDialog();\n"
" }\n"
" \n"
" public void showDialog() {\n"
" /*\n"
" * \"try\" makes sure nothing goes wrong. If something does,\n"
" * the interpreter skips to \"catch\" to see what it should do.\n"
" */\n"
" try {\n"
" /*\n"
" * The code below brings up a JOptionPane, which is a dialog box\n"
" * The String returned by the \"showInputDialog()\" method is converted into\n"
" * an integer, making the program treat it as a number instead of a word.\n"
" * After that, this method calls a second method, calculate() that will\n"
" * display either \"Even\" or \"Odd.\"\n"
" */\n"
" this.input = Integer.parseInt(JOptionPane.showInputDialog(\"Please enter a number.\"));\n"
" this.calculate();\n"
" } catch (final NumberFormatException e) {\n"
" /*\n"
" * Getting in the catch block means that there was a problem with the format of\n"
" * the number. Probably some letters were typed in instead of a number.\n"
" */\n"
" System.err.println(\"ERROR: Invalid input. Please type in a numerical value.\");\n"
" }\n"
" }\n"
" \n"
" /**\n"
" * When this gets called, it sends a message to the interpreter.\n"
" * The interpreter usually shows it on the command prompt (For Windows users)\n"
" * or the terminal (For *nix users).(Assuming it's open)\n"
" */\n"
" private void calculate() {\n"
" if ((this.input % 2) == 0) {\n"
" JOptionPane.showMessageDialog(null, \"Even\");\n"
" } else {\n"
" JOptionPane.showMessageDialog(null, \"Odd\");\n"
" }\n"
" }\n"
"}")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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 Python, please visit: https://docs.python.org/3/library/re.html