1

下面是一个文本文件,我想使用正则表达式来保存之后开始的证书

“crossCertificatePair;二进制::”

这象征着一个新证书的开始,并一直运行到最后。每个文件都有多个以相同方式开头的证书,我想要一种方法来提取每个证书并将其保存到一个变量中。

># extended LDIF
#
# LDAPv3
># base <cn=Federal Bridge CA,ou=FPKI,o=U.S. Government,c=US> with scope subtree
># filter: (objectclass=*)
># requesting: crossCertificatePair
>#

># Federal Bridge CA, FPKI, U.S. Government, US
dn: cn=Federal Bridge CA,ou=FPKI,o=U.S. Government,c=US
crossCertificatePair;binary:: MIINq6CCB2IwggdeMIIGRqADAgECAhAEBsw2n1386M2IP4F9
 Y9f9MA0GCSqGSIb3DQEBCwUAMGQxCzAJBgNVBAYTAlVTMRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxG
 TAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xIzAhBgNVBAMTGkRpZ2lDZXJ0IEZlZGVyYXRlZCBJRC
 BDQS0xMB4XDTExMTIwNzEyMDAwMFoXDTE0MTIwNzEyMDAwMFowUjELMAkGA1UEBhMCVVMxGDAWBgN
 VBAoTD1UuUy4gR292ZXJubWVudDENMAsGA1UECxMERlBLSTEaMBgGA1UEAxMRRmVkZXJhbCBCcmlk
 Z2UgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC5M26e4VUSlib2braFWKYhaUs3Q
 SR9JwuQQHZQTmuhTOQScPO7g/JAdNvTFymOIHnXrinkPmOG+YyqxQQemNdIq3yi5AAUt+I6VOhsfS
 NhZTZJsSKoNsZ72TxqOVkLMvKWNyZxvcZK3bG1wx5eErtKqlRMjTovxGXwVktB4/J+iu975SIxT1m
 IaNsNWtyQOUF3T4P7K8/u1VwPmZ2SjMNYjKnFQU7E0Vfo1OEGWUrR0KrSBUT2Vu6PSj2OwkGr5ep6
 rr+2vjboHpWG64qOoBQHyGwd7u6f/2TPkoD5OOqGdKOD4byifwi4L5arausnxY+Yy7jMM+ifG1qKD
 GgupMJjAgM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MSMwIQYDVQQDExpEaWdpQ2VydCBGZWRlcmF0ZWQgSUQgQ0EtMTCCASIwDQYJKoZIhvcNAQEBBQADg
 gEPADCCAQoCggEBAMJ4XArNzTmP4QFtzBT1MxQnTsOuyl/U6suW+5PA9BxpUEed1283vK/HatTaZ9
 WrYSM6p6h0iJF3Wc2E6LUVuHq9EfB5mYSXUadDHjkQOKmQZiZ6K2nt6ZwPFM0maD/DL7qD4ALeve3 ENxKR9/pdvewNWLLl4UWehkg8lJNuMOJ/6bLm54TNxooVzrg4mJbCVHYEqnvRZMWWTfuAisiRm9AJ
 A3E0Wb8h4tylCCYks/BddhHpa+MjZNuJ79KpxCKKPvOD6ZJ5Rz/qLESLngv5RKZ3EhlepbXh5xD3d
 +gTFW3vHdrhAK3FjhfO60U5sv/1POGpqLsaFTLwwHz76rtM0A4zygKNgf3X45tOyhI+mzaj9fM37M
 eWU=
crossCertificatePair;binary::MIIOIaCCBykwggclMIIGDaADAgECAhA1NaRHZsnFhU4oae9B
 MGFBMA0GCSqGSIb3DQEBCwUAMDYxCzAJBgNVBAYTAlVTMRAwDgYDVQQKEwdPUkMgUEtJMRUwEwYDV.....etc
4

2 回答 2

3

虽然它可以通过环视来解决,但我认为这不是正则表达式的任务:

open my $fh, '<', $filename or die $!;
my $binary = do { local $/ = undef; <$fh>; };
close $fh;

my @certs = split /\ncrossCertificatePair;binary::/, $binary;
shift @certs; # you don't need the first part
于 2012-07-10T20:11:40.390 回答
1

如果我理解了你的问题,那么我想你需要使用前瞻断言 ?=来匹配重叠的东西。

这会做到:

open my $fh, "<", $filename or die $!;
{local $/; $content = <$fh>;}
close $fh;
my $content =~ s/\s+//g;
my @certs = ($content =~ /crossCertificatePair;binary::(.*?)(?=$|crossCertificatePair;binary::)/g);

虽然它确实做了一些主要的假设,即 a) 你想丢失空格和换行符(如果没有,去掉第四行),和 b) 一旦你开始阅读证书,唯一可以接下来是另一个证书或文件的结尾。

希望这足以让你继续前进。

于 2012-07-10T19:46:19.023 回答