Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: Regex help?
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 11039 of 11531
Post > Topic >>

Re: Regex help?

by krahnj@[EMAIL PROTECTED] (John W. Krahn) May 7, 2008 at 03:46 AM

sanket vaidya wrote:
> HI all,

Hello,

> Kindly go through the code below.
> 
> use warnings;
> use strict;
> my $i=1;
> while($i<=10)
> {
> $_ = "abcpqr";
> $_=~ s/(?=pqr)/$i/;
> print "$_\n";
> $i++;
> }
> 
> Output:
> abc1pqr
> abc2pqr
> abc3pqr
> abc4pqr
> abc5pqr
> abc6pqr
> abc7pqr
> abc8pqr
> abc9pqr
> abc10pqr
> 
> The expected output is
> 
> abc001pqr
> abc002pqr
> abc003pqr
> abc004pqr
> abc005pqr
> abc006pqr
> abc007pqr
> abc008pqr
> abc009pqr
> abc010pqr
> 
> Can any one suggest me how to get that output using regex. i.e. Can this
> happen by making change in regex I used in code??

$ perl -e'
use warnings;
use strict;
for my $i ( 1 .. 10 ) {
     $_ = "abcpqr";
     $_ =~ s/(?=pqr)/sprintf q[%03d], $i/e;
     print "$_\n";
     }
'
abc001pqr
abc002pqr
abc003pqr
abc004pqr
abc005pqr
abc006pqr
abc007pqr
abc008pqr
abc009pqr
abc010pqr


Or maybe this would be better:

$ perl -e'
use warnings;
use strict;
$_ = "abcpqr";
$_ =~ s/(?=pqr)/%03d/;
for my $i ( 1 .. 10 ) {
     printf "$_\n", $i;
     }
'
abc001pqr
abc002pqr
abc003pqr
abc004pqr
abc005pqr
abc006pqr
abc007pqr
abc008pqr
abc009pqr
abc010pqr



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall
 




 3 Posts in Topic:
Regex help?
sanket.vaidya@[EMAIL PROT  2008-05-07 15:49:47 
Re: Regex help?
krahnj@[EMAIL PROTECTED]   2008-05-07 03:46:09 
Re: Regex help?
rvtol+news@[EMAIL PROTECT  2008-05-07 16:16:09 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 0:57:22 CDT 2008.