in reply to Re^2: RE - match from right
in thread RE - match from right
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
|
|---|