import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([\\-]*BEGIN\\sCERTIFICATE\\sREQUEST[\\-]+.+[-]+END\\sCERTIFICATE\\sREQUEST[-]+)";
final String string = "-----BEGIN CERTIFICATE REQUEST-----\n"
+ "MIIEuDCCAqACAQAwczELMAkGA1UEBhMCVVMxGDAWBgNVBAMMD3BsdWdpYmlsaXR5\n"
+ "LmNvbTEXMBUGA1UEBwwOU2FsdCBMYWtlIENpdHkxFDASBgNVBAoMC1BsdWdpYmls\n"
+ "aXR5MQ0wCwYDVQQIDARVdGFoMQwwCgYDVQQLDANPcmcwggIiMA0GCSqGSIb3DQEB\n"
+ "AQUAA4ICDwAwggIKAoICAQCfQqx8YexJ4HjGqIpywxvTOPDnqIaZicD3IQSUdAb5\n"
+ "5vcfAaD/0KBH/6Oa2NPlq4mYAw2octDiuklvkXIUgnn85vwlj6fLm6iR6i1tjM3g\n"
+ "tusxur3fmMdF9YqDNNM7LVSxXMq/vXuChPj3bVib2EyKJRcBn8EMTnJYdfl3YK4l\n"
+ "j+KW7cnxIy8E3DAy4KIUPbI2LNPix43emW6XuMB3hpvcbs2YpQogagAQwvNYAIcW\n"
+ "Ub3+fSEbXFNVmYFRPbd7T5Arei4Ubl8kKVWfqUnWIOdYQh7o0CbQl+67ivAyRnDU\n"
+ "oItNtPz+seWzL1xerjQqdlHrCQFGNfLdI3Hfl9SZVKOqdsjs2bEiP+C6E/pilvu/\n"
+ "zEtD5VbGd0UBBZtANm6BdBEhhigX8vL/FfiqxlPsEK31L9tQnwa6ETvcVbRQoXBW\n"
+ "idJJKCmiScz17ML8aTxw0zXc16CBHuE4AEV7xaNmdl5NsYCB+q/H3SCVsreMnu7A\n"
+ "cpCgvaCZX1/8zSWrVNMf0i+/gJCoBh1OXNnqtCPycJDPvrVGYF2U7RqzC4c91Kqe\n"
+ "QxEKHApEJAL47nFtM9c3K2fNvLEVOD+k+iDkEuL++VHaXO3VHCiVoJjQ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QaRaTh/t/laXxZgtIo\n"
+ "Y+mZN7OXk3IUv6xlO+OyneG+CU9/VOPszLK9yFf95m9l5R6E6v9r4eE9N5OMOTxT\n"
+ "SBXZ+zNnnF4bIjy747KtCVI7XJQURDBbhhXick/lgX6sNj87vMaDJDnZ9kqqgc3U\n"
+ "1H23mg7nAvcEhsYClM3Z8/bEH6eRBQ==\n"
+ "-----END PRIVATE KEY-----\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
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