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__ foo....@domain.com jack.foo....@domain.com jack.rose.foo....@domain.com best, Shaji ------------------------------------------------------------------------------- Your talent is God's gift to you. What you do with it is your gift back to God. ------------------------------------------------------------------------------- ________________________________ From: *Shaji Kalidasan* <shajiin...@yahoo.com> To: Feng He <fen...@nsbeta.info> Cc: "beginners@perl.org" <beginners@perl.org> 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__ foo....@domain.com jack.foo....@domain.com jack.rose.foo....@domain.com best, Shaji ------------------------------------------------------------------------------- Your talent is God's gift to you. What you do with it is your gift back to God. ------------------------------------------------------------------------------- ________________________________ From: Feng He <fen...@nsbeta.info> To: *Shaji Kalidasan* <shajiin...@yahoo.com> Cc: "beginners@perl.org" <beginners@perl.org> 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: foo....@domain.com jack.foo....@domain.com jack.rose.foo....@domain.com 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*" <shajiin...@yahoo.com>: 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__ >dns.supp...@dnsbed.com > > >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) <v-guo...@microsoft.com> >To: Feng He <fen...@nsbeta.info> >Cc: "beginners@perl.org" <beginners@perl.org> >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 >dns.supp...@dnsbed.com > >我猜你遇到的是转义的问题。。。 > >-----Original Message----- >From: Feng He [mailto:fen...@nsbeta.info] >Sent: Friday, December 21, 2012 3:39 PM >To: beginners@perl.org >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: dns.supp...@dnsbed.com > > > if ($string =~ /^(.*?)(?<!\\)\.(.*)$/) { > my $user = $1; > my $tld = $2; > return $user . '@'. $tld; > } > >But this won't work correctly. I got: >d...@support.dnsbed.com > >Where do I get wrong? Thanks. > >-- >To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional >commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ > > > > >