Adding a quantifier to substitution

M

mohyeldin55

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.
 
H

Hartmut Camphausen

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
 
P

Paul Lalli

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
 
M

mohyeldin55

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top