Substitution (Regular Expressions)

E

enigma

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
 
M

Mirco Wahab

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

anno4000

enigma said:
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
 
E

enigma

Thanks Alot!

That was helpful.

Enigma
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
 
A

anno4000

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
 
T

Tad McClellan

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;
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top