NAME
Regexp::Keep - filter to allow the "\K" escape in regexes
SYNOPSIS
use Regexp::Keep;
# slow and inefficient
my $r = "abc.def.ghi.jkl";
$r =~ s/(.*)\..*/$1/;
# fast and efficient
my $s = "abc.def.ghi.jkl";
$s =~ s/.*\K\..*//;
DESCRIPTION
This allows you to use the "\K" escape in your regexes, which fools the
regex engine into thinking it has only just started matching your regex.
This means you can turn the inefficient replace-with-itself construct
s/(save)delete/$1/;
into the more efficient
s/save\Kdelete//;
construct.
IMPLEMENTATION
What "\K" filters into is "(?{ Regexp::Keep::KEEP })", which is an XS
function call embedded into the regex. The function sets
"PL_regstartp[0]" to the current location in the string. This means that
"$&" now starts where "\K" is seen. That means a replacement will begin
being replaced there.
EXAMPLES
Here's are short examples to show you the abilities of "\K":
"alphabet" =~ /([^aeiou][a-z][aeiou])[a-z]/;
# $1 is "pha", $& is "phab"
"alphabet" =~ /\K([^aeiou][a-z][aeiou])[a-z]/;
# $1 is "pha", $& is "phab"
"alphabet" =~ /([^aeiou]\K[a-z][aeiou])[a-z]/;
# $1 is "pha", $& is "hab"
"alphabet" =~ /([^aeiou][a-z]\K[aeiou])[a-z]/;
# $1 is "pha", $& is "ab"
"alphabet" =~ /([^aeiou][a-z][aeiou])\K[a-z]/;
# $1 is "pha", $& is "b"
"alphabet" =~ /([^aeiou][a-z][aeiou])[a-z]\K/;
# $1 is "pha", $& is ""
BUGS
If you're using this module, you don't have a version of Perl with the
"\K" escape built-in. For shame. Upgrade.
HISTORY
0.01
Original release.
AUTHOR
Jeff "japhy" Pinyan, japhy@pobox.com
http://www.pobox.com/~japhy/
SEE ALSO
Regexp::Parts, the perlre manpage.