title: Java regex: Negative lookahead
body: <p>I'm trying to craft two regular expressions that will match URIs. These URIs are of the format: <code>/foo/someVariableData</code> and <code>/foo/someVariableData/bar/someOtherVariableData</code></p>
  
  <p>I need two regexes. Each needs to match one but not the other.</p>
  
  <p>The regexes I originally came up with are:
  <code>/foo/.+</code> and <code>/foo/.+/bar/.+</code> respectively.</p>
  
  <p>I think the second regex is fine. It will only match the second string. The first regex, however, matches both. So, I started playing around (for the first time) with negative lookahead. I designed the regex <code>/foo/.+(?!bar)</code> and set up the following code to test it</p>
  
  <pre><code>public static void main(String[] args) {
      String shouldWork = "/foo/abc123doremi";
      String shouldntWork = "/foo/abc123doremi/bar/def456fasola";
      String regex = "/foo/.+(?!bar)";
      System.out.println("ShouldWork: " + shouldWork.matches(regex));
      System.out.println("ShouldntWork: " + shouldntWork.matches(regex));
  }
  </code></pre>
  
  <p>And, of course, both of them resolve to <code>true</code>. </p>
  
  <p>Anybody know what I'm doing wrong? I don't need to use Negative lookahead necessarily, I just need to solve the problem, and I think that negative lookahead might be one way to do it.</p>
  
  <p>Thanks,</p>