Adding a quantifier to substitution

Discussion in 'Perl Misc' started by mohyeldin55@gmail.com, Jul 29, 2008.

  1. Guest

    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.
     
    , Jul 29, 2008
    #1
    1. Advertising

  2. 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
     
    Hartmut Camphausen, Jul 29, 2008
    #2
    1. Advertising

  3. Paul Lalli Guest

    On Jul 29, 10:31 am, 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
     
    Paul Lalli, Jul 29, 2008
    #3
  4. Guest

    On Jul 29, 6:32 pm, Paul Lalli <> wrote:
    > On Jul 29, 10:31 am, 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.
     
    , Jul 29, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    Greedy quantifier

    -, Jul 11, 2005, in forum: Java
    Replies:
    0
    Views:
    488
  2. Philippe Aymer
    Replies:
    12
    Views:
    220
    Philippe Aymer
    Oct 14, 2004
  3. Replies:
    19
    Views:
    424
    Dr.Ruud
    May 7, 2006
  4. Francois Massion

    nested quantifier or unrecognized escape error

    Francois Massion, Jun 2, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    185
    Mirco Wahab
    Jun 2, 2006
  5. Jack
    Replies:
    2
    Views:
    292
    Tad McClellan
    Oct 4, 2006
Loading...

Share This Page