简体   繁体   English

Perl Regex替换并保存到变量

[英]Perl Regex Replace and save to variable

$LDAP = ldap://sspdir.managed.entrust.com/ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US?cACertificate;binary,crossCertificatePair;binary 

I am trying to extract 2 strings from this code ldap directory. 我正在尝试从此代码ldap目录中提取2个字符串。 The first i want: 我想要的第一个:

$LDAP_host = sspdir.managed.entrust.com

and second... 第二...

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

My code is below, it produces constant mismatches in my output and I cannot figure out why: 我的代码在下面,它在我的输出中产生不断的不匹配,我不知道为什么:

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

I'd use: 我会用:

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

or, if you'd like to check the start of the string too: 或者,如果您也想检查字符串的开头:

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

Regards 问候

rbo 机器人

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";
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";

produces 产生

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

This should do what you want: 这应该做您想要的:

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

If you don't want change the original string you could try this: 如果您不想更改原始字符串,可以尝试以下操作:

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

Also, if you use delimiters other than // in search and replaces, you don't need to escape the forward slashes. 此外,如果在搜索和替换中使用//以外的分隔符,则无需转义正斜杠。

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

Please find below an amature way of implementing the same using perl. 请在下面找到一种使用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";

The output would be like: 输出如下:

$LDAP_base => ou=Entrust Managed Services SSP CA,ou=Certification Authorities,o=Entrust,c=US $LDAP_host => sspdir.managed.entrust.com $ LDAP_base => ou =委托托管服务SSP CA,ou =证书颁发机构,o =委托,c = US $ LDAP_host => sspdir.managed.entrust.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM