title: match all file name with specific extension except for specific file-name regex
body: <p>From the following list</p>
  <pre><code>&quot;foo.exe&quot;
  &quot;foo.dmg&quot;
  &quot;baz.exe&quot;
  &quot;this-is-another-file-name.exe&quot;
  ...
  </code></pre>
  <p>what java regex would match every strings that end with <code>&quot;.exe&quot;</code> except a specific string <code>&quot;baz.exe&quot;</code></p>
  <p>Thanks!</p>
  <p>EDIT:  I've tried  <code>(?=\.exe$)(?!baz\.exe)</code></p>
  <p><strong>UPDATE</strong> by <a href="https://stackoverflow.com/users/4807875/alexander-samoylov">user</a>. There are two solutions:</p>
  <ol>
  <li><code>(?&lt;!^baz)\.exe</code> - using negative lookbehind, the matching group is only .exe part of the word</li>
  <li><code>^(?!baz\.exe).*</code> - using negative lookahead, the matching group contain the whole file name</li>
  </ol>
  <p>Both work with &quot;grep -P&quot; and from Java (<code>\\</code> must be replace with <code>\</code>).
  Be aware also that Java's Matcher.<em>matches</em>() and Matcher.<em>find</em>() work differently, see my example:</p>
  <pre><code>import java.util.regex.*;
  public class RegexLookaround {
      public static void main(String args[]) {
          String[] strings = new String[] { &quot;bax.exe&quot;, &quot;baz.exe&quot;, &quot;baza.exe&quot;, &quot;abaz.exe&quot;, &quot;bazbaz.exe&quot; };
          Pattern p = Pattern.compile(args[0]);
          for (String s : strings) {
              Matcher m = p.matcher(s);
              if (m.matches()) {
                  System.out.println(&quot;m: &quot; + m.group());  
              }
              while (m.find()) {
                  System.out.println(&quot;f: &quot; + m.group());
              }
          }
      }
  }
  </code></pre>
  <p>Test:</p>
  <pre><code>$ java -cp . RegexLookaround '(?&lt;!^baz)\.exe'
  f: .exe
  f: .exe
  f: .exe
  f: .exe
  
  $ java -cp . RegexLookaround '^(?!baz\.exe).*'
  m: bax.exe
  m: baza.exe
  m: abaz.exe
  m: bazbaz.exe
  </code></pre>