Substitution (Regular Expressions)

Discussion in 'Perl Misc' started by enigma, Nov 2, 2006.

  1. enigma

    enigma Guest

    Hi,

    Is there a way to use the substitution operator a maximum of 'x' times
    (without using a loop or writing the command 'x' times).... I can' use
    the s/whatever/thing/g because it will replace all instances..

    Thanks
    enigma, Nov 2, 2006
    #1
    1. Advertising

  2. enigma

    Mirco Wahab Guest

    Thus spoke enigma (on 2006-11-02 14:24):

    > Is there a way to use the substitution operator a maximum of 'x' times
    > (without using a loop or writing the command 'x' times).... I can' use
    > the s/whatever/thing/g because it will replace all instances..


    One kind of a solution would be: 'count down' and
    modify the match accordingly:

    ...
    my $text = 'enigma';
    my $n = 3;

    $text =~ s/\w(??{"\^" if --$n < 0})/~/g;
    print "$text\n";
    ...

    prints:

    ~~~gma


    Regards

    M.
    Mirco Wahab, Nov 2, 2006
    #2
    1. Advertising

  3. enigma

    -berlin.de Guest

    enigma <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > Is there a way to use the substitution operator a maximum of 'x' times
    > (without using a loop or writing the command 'x' times).... I can' use
    > the s/whatever/thing/g because it will replace all instances..


    Without a loop? I don't think so, unless you want to use code insertions
    in your pattern. Here is one way:

    my $pat = 'a.';
    my $repl = 'AA';
    my $n = 3;

    my $str = 'aabbacbdadbeaf';

    $str =~ /$pat/g for 1 .. $n;
    substr( $str, 0, pos $str) =~ s/$pat/$repl/g;

    print "$str\n";

    The loop sets pos( $str) to the point after the $n-th match. Then
    you can use global substitution on that part of the string.

    Anno
    -berlin.de, Nov 2, 2006
    #3
  4. enigma

    enigma Guest

    Thanks Alot!

    That was helpful.

    Enigma
    -berlin.de wrote:
    > enigma <> wrote in comp.lang.perl.misc:
    > > Hi,
    > >
    > > Is there a way to use the substitution operator a maximum of 'x' times
    > > (without using a loop or writing the command 'x' times).... I can' use
    > > the s/whatever/thing/g because it will replace all instances..

    >
    > Without a loop? I don't think so, unless you want to use code insertions
    > in your pattern. Here is one way:
    >
    > my $pat = 'a.';
    > my $repl = 'AA';
    > my $n = 3;
    >
    > my $str = 'aabbacbdadbeaf';
    >
    > $str =~ /$pat/g for 1 .. $n;
    > substr( $str, 0, pos $str) =~ s/$pat/$repl/g;
    >
    > print "$str\n";
    >
    > The loop sets pos( $str) to the point after the $n-th match. Then
    > you can use global substitution on that part of the string.
    >
    > Anno
    enigma, Nov 2, 2006
    #4
  5. enigma

    -berlin.de Guest

    <-berlin.de> wrote in comp.lang.perl.misc:
    > enigma <> wrote in comp.lang.perl.misc:
    > > Hi,
    > >
    > > Is there a way to use the substitution operator a maximum of 'x' times
    > > (without using a loop or writing the command 'x' times).... I can' use
    > > the s/whatever/thing/g because it will replace all instances..

    >
    > Without a loop? I don't think so, unless you want to use code insertions
    > in your pattern. Here is one way:
    >
    > my $pat = 'a.';
    > my $repl = 'AA';
    > my $n = 3;
    >
    > my $str = 'aabbacbdadbeaf';
    >
    > $str =~ /$pat/g for 1 .. $n;
    > substr( $str, 0, pos $str) =~ s/$pat/$repl/g;
    >
    > print "$str\n";
    >
    > The loop sets pos( $str) to the point after the $n-th match. Then
    > you can use global substitution on that part of the string.


    Well, there *is* an alternative without a (visible) loop:

    $str =~ s/($pat)/-- $n >= 0 ? $repl : $1/eg;

    Instead of code insertions in the pattern it uses code evaluation
    on the replacement side. It works, but unlike the solution above
    it does a lot of unnecessary work when there are many instances
    of the pattern that are not to be replaced.

    Anno
    -berlin.de, Nov 2, 2006
    #5
  6. -berlin.de <-berlin.de> wrote:
    > enigma <> wrote in comp.lang.perl.misc:
    >> Hi,
    >>
    >> Is there a way to use the substitution operator a maximum of 'x' times
    >> (without using a loop or writing the command 'x' times).... I can' use
    >> the s/whatever/thing/g because it will replace all instances..

    >
    > Without a loop? I don't think so, unless you want to use code insertions
    > in your pattern.



    or by inserting code in your replacement. :)


    > Here is one way:
    >
    > my $pat = 'a.';
    > my $repl = 'AA';
    > my $n = 3;
    >
    > my $str = 'aabbacbdadbeaf';
    >
    > $str =~ /$pat/g for 1 .. $n;
    > substr( $str, 0, pos $str) =~ s/$pat/$repl/g;



    $str =~ s/$pat/ $_++ >= $n ? $& : $repl/ge;


    > print "$str\n";



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 2, 2006
    #6
    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. Jay Douglas
    Replies:
    0
    Views:
    593
    Jay Douglas
    Aug 15, 2003
  2. Vibha Tripathi

    Regular Expression for pattern substitution

    Vibha Tripathi, Jul 1, 2005, in forum: Python
    Replies:
    3
    Views:
    304
    Devan L
    Jul 1, 2005
  3. Bernd Muent
    Replies:
    2
    Views:
    504
    Bernd Muent
    Feb 15, 2006
  4. Ne Scripter
    Replies:
    8
    Views:
    99
    Rob Biedenharn
    Sep 4, 2009
  5. Noman Shapiro
    Replies:
    0
    Views:
    220
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page