Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Matlab project which I'm working on in the OSX editor TextMate. I need to find all instances of a certain word, let's say it's "foo", that is not either preceded by a "." or succeeded by a "/".

However, I cannot find any way to search for regular expressions that are negatively defined like this. Does anyone know if it is possible to search for something like "A preceded by anything other than B"?

(TextMate uses the Oniguruma regular expression library by K. Kosako.)

share|improve this question
up vote 7 down vote accepted

You want to use this bit of the syntax:

(?=subexp)         look-ahead
(?!subexp)         negative look-ahead
(?<=subexp)        look-behind
(?<!subexp)        negative look-behind

In your case, (?<!\.)foo(?!/)

share|improve this answer
Awesome! I overlooked those in the documentation. Thanks :) – Nagel Sep 20 '12 at 12:33

The ^ (circumflex or caret) inside square brackets negates the expression. So to find a "foo" not preceded by a "." would be:

share|improve this answer
that should be (^|[^.])(foo) and also it also matched one extra character. – richard Sep 20 '12 at 12:25
Thanks, Brian! This is the variant I tried initially, but I messed up the syntax and tried ^[\.] instead of [^\.] :P – Nagel Sep 20 '12 at 12:34
This won't work. You want all characters except '.' which means 'any character'. You need to escape it: (^|[^\.])(foo) – Andras Gyomrey May 8 '14 at 18:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.