title: extracting all expansions from python's glob
body: <p>Python's glob module allows to specify wildcards to list files, and it's very practical getting <em>the files</em>.</p>
  
  <p>But how can I get/reconstruct the values that were matched by the wildcards?</p>
  
  <p>For example, say that I have these 8 files: <code>fa1 fa2 fa3 fb1 fb3 fc1 fc2 fc3</code> (note: <code>fb2</code> is missing).</p>
  
  <p>I can do</p>
  
  <pre><code>import glob
  glob.glob('f[ab][12]') # ['fa2', 'fb1', 'fa1']
  </code></pre>
  
  <p>In this case, I have 2 wildcards: <code>[ab]</code> and <code>[12]</code>. Each of them matches the values <code>a</code>, <code>b</code> and <code>1</code> and <code>2</code>, but only 3 combinations of these values come out, because one file, <code>fb2</code> (a valid combination of wildcards`) does not exist.</p>
  
  <p><strong><em>Question:</em></strong> how can I get the list of the <em>valid</em> matching values for each wildcard? More precisely: how do I get the list of tuples of (string) values that matched to actually existing files?</p>
  
  <p>In my example, I would like to get the list of tuples: <code>[('a', '2'), ('b', '1'), ('a', '1')]</code>.</p>
  
  <p>Note that: </p>
  
  <ol>
  <li>I don't want to get full names, just the values matched by wildcards (in my example, the prefix <code>'f'</code> is not part of the wildcard, therefore I don't want to get it in the list of tuples);</li>
  <li>This must work for all the supported wildcards, including * and ?.</li>
  </ol>
  
  <p>The only solution I can come up with is to use regex, but this basically means to re-implement the whole glob mechanism to extract intermediate data.</p>
  
  <h1>EDIT</h1>
  
  <p>Since I got a close proposal due to "too broad" question (???), I reformulate the question as: is it possible to get that result using glob/fnmatch modules and not using directly regex?</p>