我需要做的是更改一个字符串,例如“CN = bobvilla,OU = People,DC = example,DC = com”(字符串中可以有很多DC =')到“example.com”< / p>
我有这种方法,但对我来说似乎很草率,想知道是否有人有更好的主意。
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";
感谢〜
答案 0 :(得分: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 :(得分:1)
这应该做的工作:
my $str = "DC=example, DC=com";
$str =~ s/DC=//g;
$str =~ s/,\s/./g;
print "new: $str\n";
答案 2 :(得分: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);
答案 3 :(得分:0)
在一个正则表达式中:
$str =~ s/(?:^|(,)\s+)(?:.(?<!\sDC=))*?DC=(?=\w+)|[,\s]+.*$|^.*$/$1&&'.'/ge;