in reply to matching operator question

I don't see what makes the second b in your example the "last" matched character. Matched against what? What is the matching criterion you are using?

Update: BTW, your post suggests that you do not understand the syntax for the s/// operator, which suggests that you don't understand Perl regular expressions at all. I doubt that any answer you get will do you any good until you understand at least the rudiments of regular expressions in Perl. Please read perlretut. You will probably be able to answer your own question then.

the lowliest monk

Replies are listed 'Best First'.
Re^2: matching operator question
by redss (Monk) on May 13, 2005 at 20:57 UTC
    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.

      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