pos() doesn't work because if the string only contains a prefix of the given expression (what will be the hot case I'm looking for) I will get undef and not the position I need or do I overlook sth. here? Once again in english, please?
Any alternative suggestion? Not really. To remove a prefix requires a regex match. And then you do real matching. I doubt there is any savings to be had by matching twice ... or actually cutting the string, even with pos
my $search="AB.*Z";
my $string="WWWA";
my $search_prefix = $1 if $search =~ /^(\w+)/g;
warn $search_prefix;
my $prefix_offset = index ( $string, $search_prefix );
substr $string, 0, $prefix_offset , '';
warn $string;
$string = "WWWADBBBABC";
$prefix_offset = index ( $string, $search_prefix );;
warn $string;
substr $string, 0, $prefix_offset , '';
warn $string;
$string = "WWWADBBBABC";
pos( $string ) = $prefix_offset;
warn pos( $string );
## next match m//g starts at offset
__END__
AB at jank line 5.
A at jank line 8.
WWWADBBBABC at jank line 11.
ABC at jank line 13.
8 at jank line 16.
For some idea why I think so , maybe , see Why does global match run faster than none global?, Multiple Regex evaluations or one big one? |