【问题标题】:Perl Regex Replace and save to variablePerl Regex 替换并保存到变量
【发布时间】:2012-07-10 17:55:57
【问题描述】:
$LDAP = ldap://sspdir.managed.entrust.com/ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US?cACertificate;binary,crossCertificatePair;binary 

我正在尝试从此代码 ldap 目录中提取 2 个字符串。我想要的第一个:

$LDAP_host = sspdir.managed.entrust.com

第二个……

$LDAP_base = ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US

我的代码在下面,它在我的输出中产生了不断的不匹配,我不知道为什么:

my $LDAP_host = $LDAP;
my $LDAP_base = $LDAP;
$LDAP_host =~ s|^ldap:\/\/(.*)\/|$1|i;
$LDAP_base =~ s|"\/"(.*)\?|$1|i;

【问题讨论】:

  • 如果这是一个LDAP字符串,应该有一个合适的模块来解析它。

标签: regex perl variables extract


【解决方案1】:

我会使用:

 my ($LDAP_host, $LDAP_base) = $LDAP=~ m{ // ([^/]+) / (ou=[^?]+) }x;

或者,如果您也想检查字符串的开头:

 my ($LDAP_host, $LDAP_base) = $LDAP=~ m{ ^ldap:// ([^/]+) / (ou=[^?]+) \? }x;

问候

rbo

【讨论】:

    【解决方案2】:
    my $str = "ldap://sspdir.managed.entrust.com/ou=Entrust Managed Services SSP CA,ou=Certification    Authorities,o=Entrust,c=US?cACertificate;binary,crossCertificatePair;binary";
    my ($LDAP_host, $LDAP_base) = ($str =~ m!ldap://([^/]+)/([^?]+)!);
    print "$LDAP_host  $LDAP_base\n";
    

    【讨论】:

      【解决方案3】:
      use strict;
      use warnings;
      
      my $LDAP='ldap://sspdir.managed.entrust.com/ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US?cACertificate;binary,crossCertificatePair;binary';
      
      my($LDAP_host, $LDAP_base) = $LDAP =~ m{ldap://([^/]+?)/(.*?)\?.*};
      print $LDAP_host, "\n";
      print $LDAP_base, "\n";
      

      生产

      sspdir.managed.entrust.com
      ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US
      

      【讨论】:

        【解决方案4】:

        这应该做你想做的:

        my $LDAP_host = $LDAP;
        my $LDAP_base = $LDAP;
        $LDAP_host =~ s|^ldap:\/\/(.*)\/.*|$1|i;
        $LDAP_base =~ s|^ldap:\/\/.*\/(.*)\?.*|$1|i;
        

        【讨论】:

          【解决方案5】:

          如果您不想更改原始字符串,可以试试这个:

          my ($host) = $LDAP =~ /^ldap:\/\/(.*)\//i;
          

          此外,如果您在搜索和替换中使用 // 以外的分隔符, 您不需要转义正斜杠。

          $LDAP_host =~ s{^ldap://(.*)/.*}{$1}i;
          

          【讨论】:

            【解决方案6】:

            请在下面找到一种使用 perl 实现相同功能的成熟方法。

            my $LDAP = "ldap://sspdir.managed.entrust.com/ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US?cACertificate;binary,crossCertificatePair;binary";
            $LDAP =~ '^\w+\W+(.*)/(.*)\?.*$';
            $LDAP_host = $1;
            $LDAP_base = $2;
            print "\$LDAP_base => $LDAP_base\n\$LDAP_host => $LDAP_host\n";
            

            输出如下:

            $LDAP_base => ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US $LDAP_host => sspdir.managed.entrust.com

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-05-04
              • 1970-01-01
              • 1970-01-01
              • 2021-07-11
              • 2014-09-30
              • 1970-01-01
              相关资源
              最近更新 更多