my2cents

8. June, 2007

Regex: match last occurrence

Filed under: Software Development — frightanic @ 22:33

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.

About these ads

4 Comments

  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”

    Comment by alexis779 — 12. March, 2009 @ 01:59

    • Thanks, this is a more elegent way imo and works great :)

      Comment by Bala — 16. December, 2010 @ 20:13

  2. Thank you very much, it’s work perfectly.
    Can you explain it, please? I want to understand about regex: foo(?!.*foo)

    Comment by banhbaochay — 23. November, 2010 @ 15:17

  3. Thanks for posting this, very helpful :)

    Comment by Michael Robinson — 21. September, 2011 @ 05:36


RSS feed for comments on this post.

The Rubric Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: