title: How to get best match using java.util.regex.Pattern
body: <p>Here is my use case. I have different file processing modules which is invoked based on the file name. So if the filename matches the pattern associated with a certain module that module will pick up the file.
  I have a catch all pattern defined which is used to do default processing, but this pattern should only kick in if I haven't got a better match.</p>
  
  <p>Consider the following scenario</p>
  
  <pre><code>Pattern 1 -    Sample_[0-9]*.xls  
  Pattern 2 -    [a-zA-Z]*_[0-9]*.xls
  </code></pre>
  
  <p>Now given a file <code>"Sample_11"</code>, I want Pattern 1 to be applied as its a better match than Pattern 2, however the method  <code>java.util.regex.Pattern.matcher().matches()</code> just returns true or false.
  Is there any way to identify what is the better match?</p>
  
  <p>EDIT:
  The patterns are defined outside the system (this is a weird use case), so I cannot order 
  them as suggested by many. In a sense I am looking infer the results of matching to decide if that is the best match or not. Hope this clarifies my question.</p>
  
  <p>Thanks,</p>
  
  <p>Raam</p>