import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(^\\s*(?P<att_name>[^:\\n]+):\\s*?(?P<att_value>.+)?)$";
final String string = "Certificate:\n"
+ " Data:\n"
+ " Version: 1 (0x0)\n"
+ " Serial Number: 1 (0x1)\n"
+ " Signature Algorithm: sha256WithRSAEncryption\n"
+ " Issuer: O=NetApp Inc,, CN=Mediator CA\n"
+ " Validity\n"
+ " Not Before: Jan 14 15:28:02 2020 GMT\n"
+ " Not After : Jan 13 15:28:02 2021 GMT\n"
+ " Subject: O=NetApp Inc,, CN=Mediator Server\n"
+ " Subject Public Key Info:\n"
+ " Public Key Algorithm: rsaEncryption\n"
+ " Public-Key: (2048 bit)\n"
+ " Modulus:\n"
+ " 00:e3:dd:79:69:41:23:85:6b:d1:3f:74:0e:1d:c2:\n"
+ " 13:d1:54:c9:17:cb:8f:d9:b3:87:1d:26:dd:09:ba:\n"
+ " eb:53:3d:17:cf:f6:c4:71:14:61:3c:55:85:a2:59:\n"
+ " 78:c9:8f:0f:9e:b2:c9:73:06:13:5c:7b:55:35:18:\n"
+ " 30:86:3c:f3:a1:69:8a:b6:fd:17:45:ab:0c:64:76:\n"
+ " ";
final String subst = "---\\n\\3";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
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