title: Java Regex file extension
body: <p>I have to check if a file name ends with a gzip extension. In particular I'm looking for two extensions: ".tar.gz" and ".gz". I would like to capture the file name (and path) as a group using a single regular expression excluding the gzip extension if any.
  I tested the following regular expressions on this example path</p>
  
  <pre><code>String path = "/path/to/file.txt.tar.gz";
  </code></pre>
  
  <ol>
  <li><p>Expression 1:</p>
  
  <pre><code>String rgx = "(.+)(?=([\\.tar]?\\.gz)$)";
  </code></pre></li>
  <li><p>Expression 2:</p>
  
  <pre><code>String rgx = "^(.+)[\\.tar]?\\.gz$";
  </code></pre></li>
  </ol>
  
  <p>Extracting group 1 in this way:</p>
  
  <pre><code>Matcher m = Pattern.compile(rgx).matcher(path);           
  if(m.find()){
     System.out.println(m.group(1));
  }
  </code></pre>
  
  <p>Both regular expressions give me the same result: <code>/path/to/file.txt.tar</code> and not <code>/path/to/file.txt</code>.
  Any help will be appreciated.</p>
  
  <p>Thanks in advance</p>