in reply to Re: matching operator question
in thread matching operator question

Sorry, I do understand regex's but I screwed up my question - I meant to have said I need a way for $foo =~ s/a/X/ to match the LAST letter a, not the first.

And now I see that I can just reverse the string first, then reverse it again after the replace.

I thought maybe there was an option for the s operation that would go in reverse order (like how the g option makes it global in s/a/X/g )

Thanks for your replies.

Replies are listed 'Best First'.
Re^3: matching operator question
by tlm (Prior) on May 13, 2005 at 21:41 UTC

    If you're matching a simple string (e.g. 'b'), as opposed to a general regexp pattern, then I think the rindex solution proposed by Transient and Animator is the way to go. For more complicated matches, you could use a negative look-ahead assertion to single out the last occurrence; e.g. to replace the last occurrence of the pattern /[AB]/ with X, you could use

    s/[AB](?!.*[AB])/X/s
    This will replace the last occurrence of A or B with X. The /s modifier is necessary in case the string contains embedded newlines.

    the lowliest monk