title: Regex to match any .config file with a few exceptions
body: <p>I'm trying to get a regex working to use in an .hgignore file that will ignore various copies of .config files made during debugging.</p>
  
  <p>The regex should match any path ending in <code>.config</code> as long as the path does not start with <code>_config</code>, <code>config</code>, or <code>packages</code> and as long as the file name (the characters immediately following the last slash) is not <code>app</code>, <code>web</code>, <code>packages</code>, or <code>repositories</code> (or web.release, web.debug).</p>
  
  <p>The closest I seem to get is</p>
  
  <pre><code>^(?!(_config|[Cc]onfig|packages)).*\/(?!([Aa]pp|[Ww]eb|packages|repositories)\.).*config$
  </code></pre>
  
  <p>This will properly ignore <code>Data/app.config</code>, and seems to work with all other cases, but it will incorrectly match <code>Libraries/Data/app.config</code>. When I check this out at <a href="http://regex101.com/" rel="nofollow">http://regex101.com/</a> it shows me that the <code>.*\/</code> group is only matching through <code>Libraries/</code>, not <code>Libraries/Data/</code> as I expected.</p>
  
  <p>I tried changing it to</p>
  
  <pre><code>^(?!(_config|[Cc]onfig|packages))(.*\/)*(?!([Aa]pp|[Ww]eb|packages|repositories)\.).*config$
  </code></pre>
  
  <p>But then the group <code>(.*\/)*</code> seems to match the whole path for any <code>.config</code> file.</p>
  
  <p>If I change the last negative lookahead to a matching group like so</p>
  
  <pre><code>^(?!(_config|[Cc]onfig|packages))(.*\/)(([Aa]pp|[Ww]eb|packages|repositories)\.).*config$
  </code></pre>
  
  <p>Then the <code>(.*\/)</code> matches <code>Libraries/Data/</code>, which is what I want and expected, but it appears the negative lookahead changes the matching behavior of <code>(.*\/)</code>.</p>
  
  <p>I'm not sure where to go from here? The conditions I'm trying to match or not match don't seem that complicated, but I'm not the most experienced with regexes. Maybe there is a simpler way to achieve the same thing in .hgignore?</p>
  
  <p>These are examples of paths that should match and be ignored:</p>
  
  <ul>
  <li>Web/smtp.config</li>
  <li>Libraries/Data/connectionStrings.config</li>
  </ul>
  
  <p>These are examples of paths that should NOT match and not be ignored</p>
  
  <ul>
  <li>_config/staging/smtp.config</li>
  <li>Web/web.config</li>
  <li>Web/web.release.config</li>
  <li>Web/Views/web.config</li>
  <li>Libraries/Data/app.config</li>
  <li>Libraries/Data/packages.config</li>
  <li>Data/app.config</li>
  <li>packages/MiniProfiler.EF6.3.0.11/lib/net40/MiniProfiler.EntityFramework6.dll.config</li>
  <li>packages/repositories.config</li>
  </ul>