in reply to RE - match from right

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.

Replies are listed 'Best First'.
Re^2: RE - match from right
by davido (Cardinal) on Oct 06, 2004 at 18:25 UTC
    neganch => sub { my $string = "abababa"; $string =~ s/(b)(?!.*b)$/x/; return; }

    That neganch test will fail if 'b' isn't the last character in the string, which it isn't.


    Dave

Re^2: RE - match from right
by shenme (Priest) on Oct 06, 2004 at 16:41 UTC
    I wonder if it might be even faster without _capturing_ parentheses?
    Changing $string =~ s/(b)(?!.*b)$/x/; to this $string =~ s/b(?!.*b)$/x/;

      Both of those regexps fail when 'b' isn't the last character in the string, because the negative lookahead assertion is zero-width, and yet you're anchoring to the RHS of the string.

      Look at the following code and you'll see:

      use strict; use warnings; my $orig_string = 'abababa'; my @tests = ( qr/(b)(?!.*b)$/ , qr/b(?!.*b)$/ , qr/b(?!.*b.*$)/ , qr/b(?=[^b]*$)/ ); foreach my $test_re ( @tests ) { my $scratch = $orig_string; print "$test_re\tdid", $scratch =~s/$test_re/x/ ? ' ' : "n't " , "match '$orig_string', yielding '$scratch'\n"; }

      The problem with your regexps is that they require 'b' to be the last thing in the string. This is because the lookahead assertion, positive or negative, is zero-width. If you move the anchor inside the assertion, it would improve, but it becomes kludgy, and IMO, not as clearly defined if you insist on using negative lookahead (see the third test regexp). In the fourth regexp, you can see the use of a positive lookahead and a negated character class, which, to me, is clearer, harder to break, etc.


      Dave

[OT] : perl 5.8 compared to perl 5.6
by zejames (Hermit) on Oct 08, 2004 at 12:09 UTC
    When having a look at the benchmarks above, I notice that perl 5.6 is faster than perl 5.8 for that kind of operation. I remember having heard something about it by the way.

    Is it more general ? Is perl 5.6 faster than perl 5.8 ? Do some of you hesitate to use perl 5.8 in real life when high performance is needed ?

    Just curious.


    --
    zejames