Re: Adding a quantifier to substitution



On Jul 29, 6:32 pm, Paul Lalli <mri...@xxxxxxxxx> wrote:
On Jul 29, 10:31 am, mohyeldi...@xxxxxxxxx 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.
.



Relevant Pages

  • Re: Source Code Analyzing
    ... The main reason is that I want to change the Perl code slightly without ... The B framework is geared to transform Perl code ... without changing the meaning ... string on myself if I want to figure out the x is used inside this string. ...
    (comp.lang.perl.misc)
  • Re: Extracting a character from a string
    ... in perl code I can do this: ... character from the string ... Is there something similiar I can do in kSH. ...
    (comp.unix.shell)
  • Re: Passing a regex reference through a hashed value to the right side of s///g
    ... A less cluttered example string would make it easier to see what ... The normal action is to interpolate the string value. ... The action with /e is to interpret the replacement ... but then the string must *be* interpretable Perl code. ...
    (comp.lang.perl.misc)
  • Re: setting a variable from a string
    ... > The variables name is stored as a string in another variable. ... It adds the "feature" that users can set arbitrary variables and run whatever ... It also makes perl code a real joy to read when trying ...
    (comp.lang.perl.misc)
  • Re: regexp question
    ... How to find all occurences of a substring in a string? ... convert the following Perl code to Python. ...
    (comp.lang.python)