1
$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. 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;
1
  • If this is an LDAP string, there should be a suitable module to parse it. Commented Jul 10, 2012 at 18:27

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

produces

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

Comments

0

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;

Comments

0

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;

Comments

0

Please find below an amature way of implementing the same using 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.