in reply to matching operator question

I'm a bit surprised that no one has suggested the standard greedy solution:
s/(.*)b/$1X/
Depending on how many b's there are and where the last one is, that can be a lot more efficient than all those lookaheads. It also generalizes to patterns larger than a character, when the reverse solution would have to reverse the pattern as well, and the rindex wouldn't be able to pattern match at all. All in all, I'd say it's a pretty useful idiom to have in your toolbox.

Replies are listed 'Best First'.
Re^2: matching operator question
by Tanktalus (Canon) on May 13, 2005 at 23:38 UTC

    Surprised? Really? When the default response to most regular expression questions here seems to be "that is too greedy - insert a ? after the * or +, and then it'll work fine"? We've been burned by too much greediness, so it's really of little surprise that we don't think outside of our boxes ;-)