tags:

views:

113

answers:

4

What I need to do is change a string such as "CN=bobvilla, OU=People, DC=example, DC=com" (can have many DC='s in the string) to "example.com"

I have this method but It seems sloppy to me and wanted to see if anyone had a better idea.

my $str = "CN=bobvilla, OU=People, DC=example, DC=com";
print "old: $str\n";
while($str =~ s/DC=([^,]+)//)
{
    $new_str .= "$1.";
}
$new_str =~ s/\.$//;
print "new: $new_str\n";

thanks~

+1  A: 

This should do the job:

my $str = "DC=example, DC=com";
$str =~ s/DC=//g;
$str =~ s/,\s/./g;
print "new: $str\n";
David Dorward
Would get into trouble if you had any other ",<whitespace>" match outside the DC fields
nik
Check my edit, forgot to include something
Ah, the fields which didn't exist in the original sample
David Dorward
+1  A: 

here's one way

my $str = "CN=bobvilla, OU=People, DC=example, DC=com";
@s = split /,\s+/ , $str;
foreach my $item (@s){
    if ( index($item,"DC") == 0) {        
        $item = substr($item,3);
        push(@i , $item)
    }
}
print join(".",@i);
ghostdog74
this is correct for an LDAP binding string.
nik
+4  A: 

It's relatively simple:

my $str = "CN=bobvilla, OU=People, DC=example, DC=com";
print "old: $str\n";

This was straight from question.

Now we need to get all DCs:

my @DCs = $str =~ m/DC=([^\s,]+)/g;

Combine it into result and print:

my $new_str = join '.', @DCs;
print "new: $new_str\n";

Whole "program":

my $str = "CN=bobvilla, OU=People, DC=example, DC=com";
print "old: $str\n";

my @DCs = $str =~ m/DC=([^\s,]+)/g;
my $new_str = join '.', @DCs;

print "new: $new_str\n";
depesz
A: 

In a single regex:

$str =~ s/(?:^|(,)\s+)(?:.(?<!\sDC=))*?DC=(?=\w+)|[,\s]+.*$|^.*$/$1&&'.'/ge;
Inshallah