具有特殊字符的CSR主题提取

时间:2015-07-08 03:31:26

标签: regex perl openssl csr

我需要提取$ subj,如下面的代码段所示,但看起来我的Regex并没有按预期工作。这实际上与此类似:How to string manipulate/extract subject contents in a CSR using OpenSSL command + Perl?但在CSR中具有不同的主题条目。我不确定我的正则表达式是否为%subjinfo

做了一件好事
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my $subj =
    'subject=/O=test~@#$^()_+-=\{}|;':",./<>/OU=test~@#$^()_+-=\{}|;':",./<>/emailAd‌​dress=test~@#$^()_+-=\{}|;':",./<>/L=CDE/ST=ABC/C=AU/CN=test~@#$^()_+ -=\{}|;':",./<>';

my %subjinfo = ( $subj =~ m,(\w+)=([^=]*)(?:/|$),g );
print Dumper \%subjinfo;

因此必须给出结果:

$VAR1 = {
          'subject' => '',
          'L' => 'NYC',
          'C' => 'AMER',
          'OU' => 'test~@#$^()_+-=\{}|;':",./<>',
          'emailAddress' => 'test~@#$^()_+-=\{}|;':",./<>',
          'ST' => 'AMER',
          'CN' => 'test~@#$^()_+-=\{}|;':",./<>',
          'O' => 'test~@#$^()_+-=\{}|;':",./<>'
        };

这可能吗?你能建议吗?

1 个答案:

答案 0 :(得分:3)

正则表达式上的拆分看起来比仅使用正则表达式解决方案更自然,

use Data::Dumper;

my $subj = q(subject=/O=test~@#$^()_+-=\{}|;':",./<>/OU=test~@#$^()_+-=\{}|;':",./<>/emailAddress=test~@#$^()_+-=\{}|;':",./<>/L=CDE/ST=ABC/C=AU/CN=test~@#$^()_+ -=\{}|;':",./<>);

(undef, my %subjinfo) = split m|/?(\w+)=|, $subj;

print Dumper \%subjinfo;

输出

$VAR1 = {
      'emailAddress' => 'test~@#$^()_+-=\\{}|;\':",./<>',
      'CN' => 'test~@#$^()_+ -=\\{}|;\':",./<>',
      'OU' => 'test~@#$^()_+-=\\{}|;\':",./<>',
      'L' => 'CDE',
      'C' => 'AU',
      'ST' => 'ABC',
      'subject' => '',
      'O' => 'test~@#$^()_+-=\\{}|;\':",./<>'
    };