in reply to Re^3: Efficient walk/iterate along a string
in thread Efficient walk/iterate along a string
I'm disappointed at only a 60% improvement! Maybe the segments are shorter than I would have guessed. This technique would give the best improvement for long strings containing few segments.
I used a look behind in the split regular expression to retain the matched character at the end of the previous segment. Use a [KR] match to drop the 'K' or 'R', or use a look ahead match (?=[KR]) to retain the matched character at the start of the segment.
Look behind and look ahead are anchors, they don't "consume" the matched characters, they just mark a position where some match condition is true. In this case we are testing for "was the last character R or K?".
True laziness is hard work