A regex to match a substring that isn't followed by a certain other substring.

Posted by Rayne on Stack Overflow See other posts from Stack Overflow or by Rayne
Published on 2010-04-13T15:48:30Z Indexed on 2010/04/13 15:53 UTC
Read the original article Hit count: 176

Filed under:

I need a regex that will match blahfooblah but not blahfoobarblah

I want it to match only foo and everything around foo, as long as it isn't followed by bar.

I tried using this: foo.*(?<!bar) which is fairly close, but it matches blahfoobarblah. The negative look behind needs to match baranything and not just bar.

The specific language I'm using is Clojure which uses Java regexes under the hood.

© Stack Overflow or respective owner

Related posts about regex