Regex match last instance?

I’ve been putting in a lot of time trying to learn regular expressions, but there’s a long way to go still. I certainly don’t mind looking stuff up, but I;d like to know if it’s even possible first! What I’m looking to do is match the last instance of an expression. So, for example, in the children’s song,

 "Mary had a little lamb, little lamb, little lamb,
mary had a little lamb whose fleece was white as snow"

how would I match the last “little” but not the first three? Is that even possible? I’m just starting to check out positive and negative lookaheads, but I don’t know if that applies here. If someone could give me a pointer as to what to look for, I’d really appreciate it. Thanks!

This can be done with ordinary * or + because they’re greedy by default

$t = "Mary had a little lamb, little lamb, little lamb,
mary had a little lamb whose fleece was white as snow";

echo preg_replace("~(.*)little~s", "$1BIG BAD", $t);

You are definitely correct. I oversimplified my question, which changed things. I’m actually looking to catch the last instance of “little” to the end of the text, and allowing for returns and newlines. What I’m currently using which will catch the FIRST instance to the end is:


Not sure I’m getting you here… ‘s’ switch is responsible for how newlines match. If you try my example with and without ‘s’, you’ll see the difference.

The regex I’ve been trying to learn isn’t flavor-specific yet, i.e. PHP or PERL or anything. Mostly I’m testing doing search-and-replace stuff in text editors, but I’d like the expressions to work in whatever programming language I’d like to port them to, instead of rewriting for each language. I’ve found the [^$]* is synonymous with newline match, but isn’t dependent upon which application you’re using regex in, so it should be the same as the ‘s’ switch you mentioned. I can’t seem to find any ifo on the tilde (~) you used. Are those just the start and end of the expression, like “/” would be?

[^$]* means “any characters except dollar signs”. Metachars lose their meaning in square brackets.

" means newline in every regexp engine out there.

yes, “~” is just a delimiter.

Thank you for your help. It’s starting to set in. Sorry for my wording in the previous post. Instead of “newline match” I meant to say “dot matches newline.” But, after reading your description, that isn’t really the case…it’s just anything but a dollar sign. Most of the stuff I want to write regex for is PHP, Javascript, and Apache. Does the “s” modifier work in all of them, or just PHP…or is there a universal modifier? I read somewhere that you can use the character class [\s\S] to mean any character, but I haven’t experimented enough with it yet. I’ll go do that. Thanks for the tips. I really appreciate it. All the best…

There is no ‘s’ modifier in javascript (and IIRC apache) , so you have to use \s\S or similar to denote “everything” there.

If you’re seriously interested in learning regexps, especially cross-platform, consider buying Friedl’s book. All you need to know is there (and much more :wink: