Picky, pick! Just reset pos() and it does that. The difference between yours and my code is that the behaviour is well defined and I don't use anything marked "experimental." Whether the re engine finds an optimization which allows it to skip some execution is occasionally a problem with the sort of code that was originally proposed. An update. It pleased me to show just the added line by itself and then it occurred to me that this is better done in a continue block so that next() won't accidentally skip the positioning.
while ( ... )
{
...
}
continue
{
# Reset the position of the regex match so that it will restart
# just after the start of this match. This is done inside continue
+{}
# to safeguard itself against a next() that someone else might
# add later.
pos() = $-[0] + 1;
}
while ( /($regex)/gx )
{
push @substrings,
{ match => $1,
pos => ( pos() - length $1 ),
length => length $1,};
# Restart the checking at one character after the match started.
pos() = $-[0] + 1;
}
|