Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Substitution (Regular Expressions)

Reply
Thread Tools

Substitution (Regular Expressions)

 
 
enigma
Guest
Posts: n/a
 
      11-02-2006
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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      11-02-2006
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.
 
Reply With Quote
 
 
 
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-02-2006
enigma <(E-Mail Removed)> 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

 
Reply With Quote
 
enigma
Guest
Posts: n/a
 
      11-02-2006
Thanks Alot!

That was helpful.

Enigma
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de wrote:
> enigma <(E-Mail Removed)> 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


 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-02-2006
<(E-Mail Removed)-berlin.de> wrote in comp.lang.perl.misc:
> enigma <(E-Mail Removed)> 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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-02-2006
(E-Mail Removed)-berlin.de <(E-Mail Removed)-berlin.de> wrote:
> enigma <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should this substitution be compilable? valentin tihomirov VHDL 12 11-30-2004 03:44 PM
Substitution Problem Ashok Perl 1 07-18-2004 09:33 PM
adobe multiline substitution Justin Perl 0 12-08-2003 08:28 PM
Q: string substitution in a file Troll Perl 6 09-26-2003 01:50 PM
Converted to Mozilla but one thing missing - key macro/substitution no-spam Firefox 5 07-29-2003 08:07 PM



Advertisments