Finding the last match before a first match

Answer

Yes, this is what \G is for - it anchors a regex at the current pos()ition.

$_ = 'abcdefgabcde-FIRST-fooabcdefg-SECOND-foo';

# I assume pos()==0 initially
# Set pos() to be the end of first 'foo'
/foo/gc or die "no foo";

# Extract everything from the last 'a' before the current position
# to the current position.
/.*(a.*)\G/s or die "no a before first foo";

print "$1\n";

Brian's original insanity.

Next