Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Adding a quantifier to substitution

Reply
Thread Tools

Adding a quantifier to substitution

 
 
mohyeldin55@gmail.com
Guest
Posts: n/a
 
      07-29-2008
I was wondering if there is a work around to add a quantifier if I
need to replace a certain pattern with X multiples of certain
character or string like:

$str=~s/<pattern>/R{15}/g; -------> how to replace the pattern with
"R" repeated 15 times?

Thanks.
 
Reply With Quote
 
 
 
 
Hartmut Camphausen
Guest
Posts: n/a
 
      07-29-2008
Hi,

Am 29 Jul 2008 schrieb mohyeldin55:

> ...replace a certain pattern with X multiples of certain character or string like:

^
--'

Quite close

To repeat a string several times, use the operator 'x':

my $five_times_an_a = 'a' x 5; # gives 'aaaaa'


Read perdoc perlop

hth + mfg, Hartmut



--
------------------------------------------------
Hartmut Camphausen h.camp[bei]textix[punkt]de
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 10:31*am, (E-Mail Removed) wrote:
> I was wondering if there is a work around to add a quantifier if I
> need to replace a certain pattern with X multiples of certain
> character or string like:
>
> $str=~s/<pattern>/R{15}/g; *-------> how to replace the pattern with
> "R" repeated 15 times?


The /e modifier of a s/// changes the replacement from a string to a
piece of Perl code to evaluate. So use that modifier, and make your
replacement be code that returns a string comprised of 15 R's:

$str =~ s/<pattern>/'R' x 15/ge;

Paul Lalli
 
Reply With Quote
 
mohyeldin55@gmail.com
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 6:32*pm, Paul Lalli <(E-Mail Removed)> wrote:
> On Jul 29, 10:31*am, (E-Mail Removed) wrote:
>
> > I was wondering if there is a work around to add a quantifier if I
> > need to replace a certain pattern with X multiples of certain
> > character or string like:

>
> > $str=~s/<pattern>/R{15}/g; *-------> how to replace the pattern with
> > "R" repeated 15 times?

>
> The /e modifier of a s/// changes the replacement from a string to a
> piece of Perl code to evaluate. * So use that modifier, and make your
> replacement be code that returns a string comprised of 15 R's:
>
> $str =~ s/<pattern>/'R' x 15/ge;
>
> Paul Lalli


Thanks, that really helped.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problems with regex matching - invalid ranges, quantifier follows nothing, etc. Jack Perl Misc 2 10-04-2006 12:53 AM
nested quantifier or unrecognized escape error Francois Massion Perl Misc 6 06-02-2006 10:38 AM
Quantifier...bigger than 32766...in regex leegee@gmail.com Perl Misc 19 05-07-2006 12:25 PM
Greedy quantifier - Java 0 07-11-2005 01:34 AM
Using a variable size with the repetition quantifier Philippe Aymer Perl Misc 12 10-14-2004 05:59 PM



Advertisments