in reply to RE - match from right

Anchor to the right with the $ metacharacter. By anchoring to the right, and using a negative character class, you minimize the amount of work needed to find the last occurrence even if it's not the last thing in the string.

s/b([^b]*)$/x$1/

Update:
And the following should yield better performance by using a positive lookahead assertion (eliminating the need for capturing parens), and still anchoring to the right.

s/b(?=[^b]*$)/x/

Dave

Replies are listed 'Best First'.
Re^2: RE - match from right
by Roy Johnson (Monsignor) on Oct 07, 2004 at 14:51 UTC
    I was curious about whether the anchoring really reduced the work, so I ran a benchmark of the various suggested methods. I found some of the results surprising -- like the fact that xeger was not a lot slower than the anchored match. And anchoring the 2-step solution slowed it down.
    @strings = ( 'There was a bright man from Nantucket' ,'Abba babbles about bubble blowers' ,'Enough with the excess bs already' ,'None at all in this one'); my @c; use Benchmark 'cmpthese'; %methods = ( 'anch' => sub { s/b(?=[^b]*$)/x/ for @c=@strings}, 'xeger' => sub { $_ = reverse, s/b/x/, $_ = reverse for @c=@strings +}, 'plain' => sub { s/(.*)b/$1x/ for @c=@strings}, '2part' => sub { /.*(?=b)/g and s/\Gb/x/ for @c=@strings }, '2part-A' => sub { /b(?=[^b]*$)/g and s/\Gb/x/ for @c=@strings }, 'substr' => sub { substr($_, rindex($_, 'b'), 1) = 'x' for @c=@strin +gs}, 'neglook' => sub { s/b(?!.*b)/x/ for @c=@strings } ); cmpthese(-3, \%methods );
    Results:
    Rate plain 2part-A 2part neglook xeger anch substr plain 3208/s -- -31% -35% -44% -47% -51% -64% 2part-A 4630/s 44% -- -6% -20% -23% -29% -48% 2part 4948/s 54% 7% -- -14% -18% -24% -45% neglook 5767/s 80% 25% 17% -- -4% -12% -36% xeger 6029/s 88% 30% 22% 5% -- -8% -33% anch 6536/s 104% 41% 32% 13% 8% -- -27% substr 8985/s 180% 94% 82% 56% 49% 37% --

    Caution: Contents may have been coded under pressure.