On 2012-12-22 10:46, Feng He wrote:
You probably had $string double quoted instead of
single quoted which later results in the \ being eaten.
Thank you. The people who said the problem of double quoted string are correct,
I didn't know this item before.
This is what I really want:
use strict;
my $email1 = restore_email_from_soa('support.dnsbed.com.');
my $email2 = restore_email_from_soa('dns\.support.dnsbed.com.');
my $email3 = restore_email_from_soa('dns\.tech\.support.dnsbed.com.');
print $email1,"\n";
print $email2,"\n";
print $email3,"\n";
sub restore_email_from_soa {
my $email = shift;
$email =~ s/\.$//;
if ($email =~ /^(.*?)(?<!\\)\.(.*)$/) {
my $user = $1;
my $tld = $2;
$user =~ s/\\//g;
return $user . '@'. $tld;
}
}
The output:
supp...@dnsbed.com
dns.supp...@dnsbed.com
dns.tech.supp...@dnsbed.com
Alternative approach:
#!/usr/bin/perl -Wl
use strict;
my @emails= split /\n/, <<'EOT';
support.dnsbed.com.
dns\.support.dnsbed.com.
dns\.tech\.support.dnsbed.com.
EOT
print for @emails, '- -';
s/\.$//, # remove end dot
s/\\./\n/g, # replace escaped dots by a safe placeholder
s/\./@/, # replace the first of the remaining dots by @
s/\n/./g, # bring back the saved dots
for @emails;
print for @emails, '- -';
__END__
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/