# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(((\r?\n|\s)*[A-Za-z0-9+\/]){4})*(((\r?\n|\s)*[A-Za-z0-9+\/]){2}(=(\r?\n|\s)*){2}|((\r?\n|\s)*[A-Za-z0-9+\/]){3}(=(\r?\n|\s)*))?$"
test_str = ("\n"
" This is a good test \n"
"to check for base64 encoded \n"
"strings because it forces \n"
"the string to begin and end \n"
"in its own line==\n"
"Arriba Cachipurriana\n"
"00000000: 4e 18 ac 8a c6 a0 a2 87 6d 7a cb 6d a1 c8 5e 72 :N.......mz.m..^r\n"
"00000010: 47 e8 ad b6 ac 7b ae 1e 9d ca 1d 79 db 2d ae 29 :G....{.....y.-.)\n"
"00000020: e0 b1 b7 9c 6a eb 1e 8a d7 e8 ad c7 ac b6 17 ac :....j...........\n"
"00000030: b6 b8 a7 82 da 1b 7a 08 a7 6a 77 5e 9d d8 a7 8a :......z..jw^....\n"
"00000040: db 28 c2 79 62 9d :.(.yb.\n"
"00000046.\n"
"--\n"
"CglUaGlzIGlzIGEgZ29vZCB0ZXN0IAp0byBjaGVjayBmb3IgYmFzZTY0IGVuY29kZWQgCnN0cmlu\n"
"Z3MgYmVjYXVzZSBpdCBmb3JjZXMgCnRoZSBzdHJpbmcgdG8gYmVnaW4gYW5kIGVuZCAKaW4gaXRz\n"
"IG93biBsaW5lPT0KQXJyaWJhIENhY2hpcHVycmlhbmEKMDAwMDAwMDA6IDRlIDE4IGFjIDhhIGM2\n"
"IGEwIGEyIDg3IDZkIDdhIGNiIDZkIGExIGM4IDVlIDcyIDpOLi4uLi4uLm16Lm0uLl5yCjAwMDAw\n"
"MDEwOiA0NyBlOCBhZCBiNiBhYyA3YiBhZSAxZSA5ZCBjYSAxZCA3OSBkYiAyZCBhZSAyOSA6Ry4u\n"
"Li57Li4uLi55Li0uKQowMDAwMDAyMDogZTAgYjEgYjcgOWMgNmEgZWIgMWUgOGEgZDcgZTggYWQg\n"
"YzcgYWMgYjYgMTcgYWMgOi4uLi5qLi4uLi4uLi4uLi4KMDAwMDAwMzA6IGI2IGI4IGE3IDgyIGRh\n"
"IDFiIDdhIDA4IGE3IDZhIDc3IDVlIDlkIGQ4IGE3IDhhIDouLi4uLi56Li5qd14uLi4uCjAwMDAw\n"
"MDQwOiBkYiAyOCBjMiA3OSA2MiA5ZCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA6Ligu\n"
"eWIuCjAwMDAwMDQ2Cg==\n")
subst = "<<<$0>>>"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
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