我有一个文本文件,我在其中使用以下语法存储证书:
-----BEGIN CERTIFICATE-----
Certificate is in here. It's a really long string of characters and looks like garbage. Each certificate is variable length.
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
Another certificate is in here
-----END CERTIFICATE-----
然后,我有代码尝试读取上述文本文件,并一一检查每个证书。
//This copies all of my certificates from a file into a String
String certificates = new Scanner(new File("certificates.txt"), "UTF-8").useDelimiter("\\A").next();
//This creates a pattern so that I can examine each certificate one at a time
//(?s) allows this pattern to span several lines.
Pattern pattern = Pattern.compile("(?s)-----BEGIN CERTIFICATE-----.*-----END CERTIFICATE");
Matcher matcher = pattern.matcher(certificates);
//I attempt to examine each certificate one at a time
while(matcher.find())
{
System.out.println(matcher.group());
}
但是,当我调用 matcher.find() 时,它会返回整个证书文件。我想是因为它在文件开头找到“-----BEGIN CERTIFICATE-----”,然后在文件末尾找到“-----END CERTIFICATE-----” .
如何更改我的正则表达式模式以便它按顺序找到每个证书?