2009-07-17 69 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;