# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:(?:\n)?[^\n]*){0,22}"
test_str = ("/*\n"
" * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser\n"
" *\n"
" * Developed for use with the book:\n"
" *\n"
" * Data Structures and Algorithms in Java, Sixth Edition\n"
" * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser\n"
" * John Wiley & Sons, 2014\n"
" *\n"
" * This program is free software: you can redistribute it and/or modify\n"
" * it under the terms of the GNU General Public License as published by\n"
" * the Free Software Foundation, either version 3 of the License, or\n"
" * (at your option) any later version.\n"
" *\n"
" * This program is distributed in the hope that it will be useful,\n"
" * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
" * GNU General Public License for more details.\n"
" *\n"
" * You should have received a copy of the GNU General Public License\n"
" * along with this program. If not, see <http://www.gnu.org/licenses/>.\n"
" */\n"
"package dsaj.arrays;\n\n"
"import java.util.Arrays;\n\n"
"/** Class for doing encryption and decryption using the Caesar Cipher. */\n"
"public class CaesarCipher {\n"
" protected char[] encoder = new char[26]; // Encryption array\n"
" protected char[] decoder = new char[26]; // Decryption array\n\n"
" /** Constructor that initializes the encryption and decryption arrays */\n"
" public CaesarCipher(int rotation) {\n"
" for (int k=0; k < 26; k++) {\n"
" encoder[k] = (char) ('A' + (k + rotation) % 26);\n"
" decoder[k] = (char) ('A' + (k - rotation + 26) % 26);\n"
" }\n"
" }\n\n"
" /** Returns String representing encrypted message. */\n"
" public String encrypt(String message) {\n"
" return transform(message, encoder); // use encoder array\n"
" }\n\n"
" /** Returns decrypted message given encrypted secret. */\n"
" public String decrypt(String secret) {\n"
" return transform(secret, decoder); // use decoder array\n"
" }\n\n"
" /** Returns transformation of original String using given code. */\n"
" private String transform(String original, char[] code) {\n"
" char[] msg = original.toCharArray();\n"
" for (int k=0; k < msg.length; k++)\n"
" if (Character.isUpperCase(msg[k])) { // we have a letter to change\n"
" int j = msg[k] - 'A'; // will be value from 0 to 25\n"
" msg[k] = code[j]; // replace the character\n"
" }\n"
" return new String(msg);\n"
" }\n\n"
" /** Simple main method for testing the Caesar cipher */\n"
" public static void main(String[] args) {\n"
" CaesarCipher cipher = new CaesarCipher(3);\n"
" System.out.println(\"Encryption code = \" + new String(cipher.encoder));\n"
" System.out.println(\"Decryption code = \" + new String(cipher.decoder));\n"
" String message = \"THE EAGLE IS IN PLAY; MEET AT JOE'S.\";\n"
" String coded = cipher.encrypt(message);\n"
" System.out.println(\"Secret: \" + coded);\n"
" String answer = cipher.decrypt(coded);\n"
" System.out.println(\"Message: \" + answer); // should be plaintext again\n"
" }\n"
"}\n")
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1)
if result:
print (result)
# 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