You want to add the $ anchor in that pattern (=~ s/(b)(?!.*b)$/x/; Another approach is to use a sexeger
my $string = "abababa"; $string = reverse $string; $string =~ s/b/x/; $string = reverse $string; print $string,$/;
or if you're dealing exact strings, substr/rindex
my $string = "abababa"; my $ri = rindex( $string, 'b' ); substr( $string, $ri, 1) = 'x'; print $string,$/;
I'm not sure which is better (and that may vary from different perl versions, and/or size of string), so whats left is to benchmark.

update: and here is my benchmark. sexeger comes out on top in two different versions of perl

use Benchmark 'cmpthese'; my $sref = { neglook => sub { my $string = "abababa"; $string =~ s/(b)(?!.*b)/x/; return; }, rindex => sub { my $string = "abababa"; my $ri = rindex( $string, 'b' ); substr( $string, $ri, 1) = 'x'; return; }, sexeger => sub { my $string = "abababa"; $string = reverse $string; $string =~ s/b/x/; $string = reverse $string; return; }, }; cmpthese( 3_000_000, $sref ); __END__ perl5.8.4 Rate neglook rindex sexeger neglook 523195/s -- -21% -36% rindex 659776/s 26% -- -19% sexeger 813670/s 56% 23% -- perl5.6.2 Rate neglook rindex sexeger neglook 719252/s -- -19% -20% rindex 888889/s 24% -- -1% sexeger 901442/s 25% 1% --

UPDATE: And of course, I hadn't taken my own advice. Here is the updated benchmark:

use Benchmark 'cmpthese'; use strict; use warnings; my $sref = { neglook => sub { my $string = "abababa"; $string =~ s/(b)(?!.*b)/x/; return; }, neganch => sub { my $string = "abababa"; $string =~ s/(b)(?!.*b)$/x/; return; }, rindex => sub { my $string = "abababa"; my $ri = rindex( $string, 'b' ); substr( $string, $ri, 1) = 'x'; return; }, sxedni => sub { my $string = "abababa"; $string = reverse $string; my $ri = index( $string, 'b' ); substr( $string, $ri, 1) = 'x'; $string = reverse $string; return; }, sexeger => sub { my $string = "abababa"; $string = reverse $string; $string =~ s/b/x/; $string = reverse $string; return; }, }; $|=1; print "Revving up my CPU"; for( 1 .. 3 ){ my $foo = 0; $foo++ for 1 .. 3_000_000; $foo-- for 1 .. 3_000_000; print '.'; } print "\n"; cmpthese( 3_000_000, $sref ); __END__ perl5.8.4 Rate sxedni neglook rindex sexeger neganch sxedni 505221/s -- -6% -26% -39% -79% neglook 534855/s 6% -- -22% -35% -77% rindex 685714/s 36% 28% -- -17% -71% sexeger 824176/s 63% 54% 20% -- -65% neganch 2369668/s 369% 343% 246% 188% -- perl5.6.2 Rate sxedni neglook rindex sexeger neganch sxedni 596303/s -- -17% -33% -33% -80% neglook 716503/s 20% -- -19% -19% -76% rindex 884695/s 48% 23% -- 0% -71% sexeger 884695/s 48% 23% 0% -- -71% neganch 3003003/s 404% 319% 239% 239% --

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.


In reply to Re: RE - match from right by PodMaster
in thread RE - match from right by bangers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.