1
$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;
4

6 回答 6

3

我会使用:

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

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

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

问候

rbo

于 2012-07-10T18:08:30.720 回答
1
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
于 2012-07-10T18:11:35.033 回答
1
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";
于 2012-07-10T18:10:11.577 回答
0

如果您不想更改原始字符串,可以尝试以下操作:

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

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

$LDAP_host =~ s{^ldap://(.*)/.*}{$1}i;
于 2012-07-10T18:11:03.427 回答
0

这应该做你想要的:

my $LDAP_host = $LDAP;
my $LDAP_base = $LDAP;
$LDAP_host =~ s|^ldap:\/\/(.*)\/.*|$1|i;
$LDAP_base =~ s|^ldap:\/\/.*\/(.*)\?.*|$1|i;
于 2012-07-10T18:00:06.717 回答
0

请在下面找到一种使用 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

于 2014-03-25T15:27:37.443 回答