Regex: match last occurrence

Today, I found myself looking for a regular expression that matches only the last occurrence of a given expression. As I’m still not a regex mastermind I couldn’t come up with it just like that.

The key to the solution is a so called “negative lookahead“. A lookahead doesn’t consume characters in the string, but only asserts whether a match is possible or not. So if you wanted to extract the last “foo” in the text “foo bar foo bar foo” your regex would look like this:

foo(?!.*foo)

If you used the DOTALL option the above expression would even work correctly on a multi-line text such as

foo
bar
foo
bar
foo

Of course the example is not taken from a real life scenario as it doesn’t matter which “foo” is matched as they’re all the same anyway. The expression would with no doubt be more complicated, but I hope you get the point.

Update:

Someone asked for an explanation…Here’s what RegexBuddy, my indispensable regex tool, produces automatically:
# foo(?!.*foo)
#
# Match the characters “foo” literally «foo»
# Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*foo)»
# Match any single character that is not a line break character «.*»
# Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match the characters “foo” literally «foo»

One thought on “Regex: match last occurrence

  1. What about using a greedy quantifier and a catching group for the pattern to extract that last occurence:
    .*(foo)

    In Ruby
    either
    “foo bar foo bar foo”.match(/.*(foo)/)[1]
    or
    “foo bar foo bar foo”.match(/foo(?!.*foo)/)[0]
    will return the last “foo”

Leave a Reply