Greetings,
This uses named captures introduced in Perl version 5.10. So you need to have
Perl 5.10+
use 5.010;
while (<DATA>) {
if(/
(?<username>[\w.]+) #Match foo.bar | jack.foo.bar |
jack.rose.foo.bar
(?<at>@) #Match @
(?<domainname>\w+) #Match domain
(?<dot>\.) #Match dot
(?<topleveldomain>\w+) #Match com
/x) {
print "$+{username}$+{at}$+{domainname}$+{dot}$+{topleveldomain}\n";
#This is just in case if you want to split the username,
#otherwise please ignore these 3 lines
print "User name splitted into tokens\n";
@tokens = split /\./, $+{username};
print "[", join '-',@tokens, "]\n";
}
}
__DATA__
[email protected]
[email protected]
[email protected]
best,
Shaji
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------
________________________________
From: *Shaji Kalidasan* <[email protected]>
To: Feng He <[email protected]>
Cc: "[email protected]" <[email protected]>
Sent: Friday, 21 December 2012 3:55 PM
Subject: Re: Help with a regex
Greetings,
IMHO, Here is one way to do it. Again there could be better ways to solve this
one.
while (<DATA>) {
if(/
([\w.]+) #Match foo.bar | jack.foo.bar | jack.rose.foo.bar
(@) #Match @
(\w+) #Match domain
\. #Match dot
(\w+) #Match com
/x) {
print "$1$2$3.$4\n";
}
}
__DATA__
[email protected]
[email protected]
[email protected]
best,
Shaji
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------
________________________________
From: Feng He <[email protected]>
To: *Shaji Kalidasan* <[email protected]>
Cc: "[email protected]" <[email protected]>
Sent: Friday, 21 December 2012 3:31 PM
Subject: Re: Help with a regex
The email styles in DNS's SOA is changing.
For example,
it can be any of these:
[email protected]
[email protected]
[email protected]
Thus they appear as:
foo\.bar.domain.com.
jack\.foo\.bar.domain.com.
jack\.rose\.foo\.bar.domain.com
I wish to translate them to the regular email addresses.
Thanks.
21.12.2012, 17:20, "*Shaji Kalidasan*" <[email protected]>:
Greetings,
>
>Here is one way of doing it. I admit there will be better solutions to this.
>
>while (<DATA>) {
> if(/
> (\w+) #Match dns
> \. #Match dot
> (\w+) #Match support
> (@) #Match @
> (\w+) #Match dnsbed
> \. #Match dot
> (\w+) #Match com
> /x) {
> print "$1.$2$3$4.$5";
> }
>}
>
>__DATA__
>[email protected]
>
>
>best,
>Shaji
>-------------------------------------------------------------------------------
>Your talent is God's gift to you. What you do with it is your gift back to God.
>-------------------------------------------------------------------------------
>
>________________________________
>From: Guoke Zhou (Wicresoft) <[email protected]>
>To: Feng He <[email protected]>
>Cc: "[email protected]" <[email protected]>
>Sent: Friday, 21 December 2012 2:01 PM
>Subject: RE: Help with a regex
>
>
>$string='dns\.support.dnsbed.com';
>print "$string\n";
>if ($string =~ /^(.*?)\\\.(.*?)(?<!\\)\.(.*)$/) {
> print $1.".".$2."@".$3;
> }
>
>Output:
>dns\.support.dnsbed.com
>[email protected]
>
>我猜你遇到的是转义的问题。。。
>
>-----Original Message-----
>From: Feng He [mailto:[email protected]]
>Sent: Friday, December 21, 2012 3:39 PM
>To: [email protected]
>Subject: Help with a regex
>
>Hello,
>
>I have a string like: dns\.support.dnsbed.com I want to translate it to a
>regular email address: [email protected]
>
>
> if ($string =~ /^(.*?)(?<!\\)\.(.*)$/) {
> my $user = $1;
> my $tld = $2;
> return $user . '@'. $tld;
> }
>
>But this won't work correctly. I got:
>[email protected]
>
>Where do I get wrong? Thanks.
>
>--
>To unsubscribe, e-mail: [email protected] For additional
>commands, e-mail: [email protected] http://learn.perl.org/
>
>
>
>
>