2009-07-17 66 views
1

我需要做的是将诸如“CN = bobvilla,OU = People,DC = example,DC = com”(字符串中可以有多个DC =)的字符串更改为“example.com”如何从LDAP字段中提取完整的域名?

我有这个方法,但它似乎对我来说马虎,想看看是否有人有一个更好的主意。

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

感谢〜

回答

4

这是比较简单的:

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

这是直接的问题。

现在我们需要把所有的DC。

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

合并成结果,并打印:

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

整体 “程序”:

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

这应该做的工作:

my $str = "DC=example, DC=com"; 
$str =~ s/DC=//g; 
$str =~ s/,\s/./g; 
print "new: $str\n"; 
+0

如果你有任何其他的“,”匹配在DC字段外面 – nik 2009-07-17 14:45:36

+0

检查我的编辑,忘记包括一些东西 – user105033 2009-07-17 14:45:37

1

这里有一个方法

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); 
0

在一个单一的正则表达式:

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