import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(((\\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)*))?$";
final String string = "\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";
final String subst = "<<<$0>>>";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + 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 Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html